我使用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