EF5到EF6升级 - 导航属性破损 [英] EF5 to EF6 upgrade - navigation properties are broken

查看:402
本文介绍了EF5到EF6升级 - 导航属性破损的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用nuget将EF5升级到EF6,在我的解决方案中引入了一个突破性的变化。当我运行我的一个单元测试(它影响一切)时,我发现了这一点。在每个测试中,我init通过这样做:

  //加热EF。 
using(var context = new ReportingDbContext())
{
context.Database.Initialize(false); //< - 繁荣!
}
//初始化服务
_inventoryService = new InventoryService();

它抛出了我这个例外:

 属性EmployeeID不能配置为导航属性。该属性必须是有效的实体类型,该属性应具有非抽象的getter和setter。对于集合属性,类型必须实现ICollection< T>其中T是有效的实体类型。 

奇怪的是,这一切都是EF5上的一切。我通过我的模型去狩猎(我有一堆),发现EmployeeID到处都是生活。他们都是这样的:

  [Table(mytablename)] 
public class CSATEntity
{

[Key,DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CSATID {get;组; }

//外键
public int ClientID {get;组; }
public int ContactID {get;组;

// nav props
[ForeignKey(ClientID)]
public virtual CompanyEntity CompanyEntity {get;组; }
[ForeignKey(EmployeeID)]
public virtual EmployeeEntity EmployeeEntity {get;组; }
...更多道具

异常不会注意哪个模型被抬起,或者如果它们都是。

解决方案

尝试从


$ b $中更改命名空间b

  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 。它解释了如何升级到实体框架6


I used nuget to upgrade EF5 to EF6, and somewhere a breaking change was introduced for my solution. I discovered this when running one of my unit tests (it affects everything though). In each test, I init by doing this:

// warm up EF.
using (var context = new ReportingDbContext())
{
     context.Database.Initialize(false); // <-- boom!
}
// init the service
_inventoryService = new InventoryService();

It tosses me this exception:

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.

The strange thing this, everything was just peachy on EF5. I went hunting through my models (I have a bunch) and found everywhere that EmployeeID lives. They all look like this:

[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

The exception doesn't note which model is jacked up, or if all of them are. What is the best way to hunt this down?

解决方案

Try changing namespace from

 System.Data.Objects.ObjectContext to System.Data.Entity.Core.Objects.ObjectContext

 System.Data.Objects to System.Data.Entity.Core.Objects

Take a look at this MSDN page http://msdn.microsoft.com/en-us/data/dn469466. It explains how to upgrade to Entity Framework 6

这篇关于EF5到EF6升级 - 导航属性破损的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆