Entity Framework - “Unable to create a constant value of type…” exception

后端 未结 2 1795
执念已碎
执念已碎 2020-12-06 19:09

Can somebody help me resolve this exception:

Test method KravmagaTests.Model.Entities.StudentTest.Create_Valid_Student threw exception: System

2条回答
  •  隐瞒了意图╮
    2020-12-06 19:53

    Change your test method this way:

    // ...
    context.Save();
    int newStudentId = student.Id;
    // because the Id generated by the DB is available after SaveChanges
    
    bool exists = context.Accounts.Any(a => a.Id == newStudentId);
    Assert.IsTrue(exists);
    

    Contains doesn't work here because it checks if a particular object instance is in the context.Accounts set. Translation of this check into SQL is not supported, only for primitive types (like the exception says). Any just translates the filter expression you specify into SQL and passes it to the database.

提交回复
热议问题