【问题标题】:EF5 to EF6 upgrade - navigation properties are brokenEF5 到 EF6 升级 - 导航属性已损坏
【发布时间】:2014-03-14 06:35:03
【问题描述】:

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

该例外没有说明哪个型号被顶起,或者是否所有型号都被顶起。追捕这个问题的最佳方法是什么?

【问题讨论】:

    标签: c# entity-framework upgrade relationship


    【解决方案1】:

    尝试从

    更改命名空间
     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

    【讨论】:

    • 我的任何 using 语句中都没有类似的引用。我的存储库类使用 System.Data.Entity,当我在解决方案资源管理器中检查实际引用时,我看到 EntityFramework 和 EntityFramework.SqlServer 而没有 System.Data.Entity,所以我假设 using 语句指的是现在正确的 EF6 DLL。如果这意味着什么,我先使用代码。
    • @BillSambrone 根据这篇 msdn 文章安装 EF6 NuGet package 应该会自动从您的项目中删除对 System.Data.Entity 的所有引用 msdn.microsoft.com/en-us/data/dn469466
    • 它确实从引用中删除了它,但没有从实际类中的 using 语句中删除。当我手动删除它时,我失去了我的 DbContext 类。对于实际的 using 语句,我应该使用替代方法吗?
    • @BillSambrone 几天前,当我们将其中一个项目升级到EF6 时,我们遇到了类似的问题。将 Context.tt.cs 中的命名空间从 System.Data.Objects 更改为 System.Data.Entity.Core.Objects 修复了它。希望这会有所帮助
    • @BillSambrone - 你能解决这个问题吗?我也面临同样的问题:stackoverflow.com/questions/23334685/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2014-12-14
    • 2016-09-14
    • 1970-01-01
    相关资源
    最近更新 更多