我使用 nuget 将 EF5 升级到 EF6,并且在某处为我的解决方案引入了重大更改。我在运行我的一个单元测试时发现了这一点(尽管它会影响一切)。在每个测试中,我通过这样做来初始化:
// warm up EF.
using (var context = new ReportingDbContext())
{
context.Database.Initialize(false); // <-- boom!
}
// init the service
_inventoryService = new InventoryService();
它抛给我这个异常:
The property 'EmployeeID' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<T> where T is a valid entity type.
奇怪的是,EF5 上的一切都很完美。我搜索了我的模型(我有一堆),发现 EmployeeID 所在的每个地方。它们看起来都是这样的:
[Table("mytablename")]
public class CSATEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CSATID { get; set; }
// foreign keys
public int ClientID { get; set; }
public int ContactID { get; set; }
// nav props
[ForeignKey("ClientID")]
public virtual CompanyEntity CompanyEntity { get; set; }
[ForeignKey("EmployeeID")]
public virtual EmployeeEntity EmployeeEntity { get; set; }
... more props
异常不会说明哪个模型被顶起,或者是否所有模型都被顶起。找到它的最佳方法是什么?
最佳答案
尝试改变命名空间
System.Data.Objects.ObjectContext to System.Data.Entity.Core.Objects.ObjectContext
System.Data.Objects to System.Data.Entity.Core.Objects
看看这个 MSDN 页面 http://msdn.microsoft.com/en-us/data/dn469466 .它解释了如何升级到 Entity Framework 6
关于c# - EF5 到 EF6 升级 - 导航属性损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21787156/