c# - EF5 到 EF6 升级 - 导航属性损坏

标签 c# entity-framework upgrade relationship

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

相关文章:

.net - 关于新的 EF5 自动编译查询功能

python - 从 Tensorflow 1.8.0 升级到 1.11.0 后,每个时期都会记录 OutOfRangeError

visual-studio - 将 Visual Studio 2008 Professional 升级到 Visual Studio 2010 Premium

c# - 将 Python(pyelliptic、openssl)ecc key 转换为 c#(BouncyCaSTLe?)

c# - Asp.Net Core 中的@Html.Action

c# - `using` 带有未初始化变量的语句是不可能的,也不是最终的,那么如何正确处理呢?

javascript - 如何显示来自代码隐藏的警报消息?

c# - 带有 ASP.NET MVC 的 Entity Framework

c# - Entity Framework 6 和 Moq4 : Is it possible to have a mocked DbSet retain added data for the duration of its scope?

sql-server - 将 SQL Server 2008 升级到 2008 R2