Linq 简单查询 returns 意外结果

Linq simple query returns unexpected results

我已按如下方式配置与数据库的连接:

Web.config

<add name="MyContext" providerName="System.Data.SqlClient" connectionString="Data Source=MyServer;Initial Catalog=MyDB;User id=MyId;Password=MyPassword; MultipleActiveResultSets=true" />

然后我class创建上下文以访问数据库

    public class MyContext: DbContext
    {
        public MyContext() : base("MyContext")
        {
            Database.SetInitializer<Models.MyContext>(null);
        }

        public DbSet<MyModel> MyTable { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

然后我的数据库 table 'MyTable',这是一个历史记录 table,所以每天 6:00 我都从另一个 [=43] 插入数据=],最后我会有这样的结果

Id | Date
-----------------------------
 1 | 2018-05-30 16:01:00.332
 1 | 2018-05-31 14:21:03.456
 1 | 2018-06-01 11:45:01.316
 2 | 2018-05-30 21:44:00.544
 2 | 2018-05-31 22:45:00.987
 2 | 2018-06-01 23:46:00.769

所以现在 MyController.cs

    public IQueryable<MyModel> GetData(string Id)
    {
        var result = MyContext.MyTable.AsQueryable();

        return result.Where(w => w.Id == Id).toList();
    }

我正在尝试 return Id = 1 的所有行,在调试时,当我在 return 语句中设置断点时,它 return 是同一行,对于每个记录。

    Id | Date
    -----------------------------
     1 | 2018-05-30 16:01:00.332
     1 | 2018-05-30 16:01:00.332
     1 | 2018-05-30 16:01:00.332

编辑:我按照您的要求添加了 MyModel class

 public class MyModel
    {
        [Index(IsUnique = false)]
        public int Id { get; set; }
        public DateTime Date{ get; set; }

        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
        public string Field4 { get; set; }
        public string Field5 { get; set; }
        public string Field6 { get; set; }
        public string Field7 { get; set; }
        public DateTime? Field8 { get; set; }
        public string Field9 { get; set; }
        public string Field10 { get; set; }
        public string Field11 { get; set; }
        public string Field12 { get; set; }
        public string Field13 { get; set; }
        public string Field14 { get; set; }
        public string Field15 { get; set; }
        public string Field16 { get; set; }
        public string Field17 { get; set; }
        public string Field18 { get; set; }
        public string Field19 { get; set; }
        public int? Field20 { get; set; }
        public string Field21 { get; set; }
        public int? Field22  { get; set; }
        public int? Field23 { get; set; }
        public int? Field24  { get; set; }
        public string Field25 { get; set; }
        public string Field26 { get; set; }
        public string Field27 { get; set; }
        public int Field28 { get; set; }
        public string Field29 { get; set; }
        public string Field30 { get; set; }
        public string Field31 { get; set; }
        public string Field32 { get; set; }
        public string Field33 { get; set; }
        public DateTime? Field34 { get; set; }

    }

你能帮我弄清楚这是怎么回事吗?

谢谢。

[Index(IsUnique = false)]
public int Id { get; set; }

这不会阻止 Id 成为主键。如果是这样,您会看到另一个错误,EF 要求在您的 类 上进行 PK。

您只是在 Id 列上添加了一个额外的索引。未删除 PK 约束。

所以我们最初的怀疑似乎是正确的,您在内存中加载了一堆具有重复 PK 值的记录。当您尝试 SaveChanges() 时,您将遇到错误或丢失数据。

一个简单的修复看起来像

public class MyModel
{
    [Key]
    public int ModelId { get; set; }  // auto filled, ignore where you don't need it

    //[Index(IsUnique = false)] -- not needed
    public int Id { get; set; }       
    public DateTime Date{ get; set; }
    ...