如何解决没有主键的表在实体框架中获得相同的行
我在数据库表“ City”中有以下数据,没有主键
CityId CityName
1 ABC
1 XYZ
我正在使用entityframework来获取此数据,并且得到同一行。 下面是上下文类代码
public class StudentsDbContext :DbContext
{
public DbSet<City> cities { get; set; }
}
下面是城市等级代码
[Table("City")]
public class City
{
[Key]
public int CityId { get; set; }
public string CityName { get; set; }
}
下面是输出代码
StudentsDbContext studentsDbContext = new StudentsDbContext();
List<City> cities = studentsDbContext.cities.ToList();
foreach (var val in cities)
{
Console.WriteLine("studentName: " + val.CityName);
}
Console.ReadLine();
下面是输出图像 enter image description here
下面是期望的结果
CityName: ABC
CityName: XYZ
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。