c# - EF5 和 .NET 4.5 中的 NotMapped 错误

标签 c# code-first visual-studio-2012 .net-4.5 entity-framework-5

在 VS2012 中(.NET 4.5 和 Entity Framework 5)

暴露继承关系时,导致编译时错误:

You cannot use Ignore method on the property 'InnerString' on type 'MrTree.SubSubClass' because this type inherits from the type 'MrTree.BaseClass' where this property is mapped. To exclude this property from your model, use NotMappedAttribute or Ignore method on the base type.

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyDbcontext db = new MyDbcontext();
            int i = db.SubSubClasses.Count();
        }
    }

    public class BaseClass
    {
        [NotMapped]
        public string InnerString { get; set; }
    }

    public class SubClass : BaseClass
    {
    }

    public class SubSubClass : SubClass
    {
    }

    public class MyDbcontext : DbContext
    {
        public DbSet<SubSubClass> SubSubClasses { get; set; }
        public DbSet<SubClass> SubClasses { get; set; }

    }
}

你能告诉我出了什么问题吗?

最佳答案

我已经找到了解决这个问题的方法,我自己也遇到过。我确实发布了一些关于可能导致问题的补充信息,但它被一些自负的 Nerd 删除了,他们认为这不值得。 所以这是我对这个问题的答案 - 可能不完全正确,但它对我有用,所以请不要删除这个 Nerd 先生,以防它帮助其他人。

首先,这个问题只影响那些不直接使用 NotMapped 属性继承抽象类的类,而是影响那些从另一个类继承的类,而另一个类本身又继承自基类,根据原始声明。

以下内容对我有用,使用类似于以下类设置的内容:

基础类别:

public abstract class EntityBase
{
   public virtual int Id { get; set; } // this will be mapped
   public virtual int DoNotMap { get; set; } // this should be ignored   
}

一级继承

public class Contact : EntityBase
{
  public string Name { get; set; }
}

二级继承

public class Employee : Contact
{
  public DateTime StartDate { get; set; }
}

创建一个继承自 EntityTypeConfiguration 的通用配置类:

public class DataEntityBaseConfiguration<T> 
  : EntityTypeConfiguration<T> where T : EntityBase
{
  public DataEntityBaseConfiguration()
  {
    Ignore(x => x.DoNotMap);
  }
}

为直接从EntityBase类继承的所有一级继承创建配置类,即:

public class ContactConfiguration : DataEntityBaseConfiguration<Contact>
{
   // place any further configuration rules in the constructor
}

解决方法:任何间接从 EntityBase 继承的类,只需创建一个从 EntityTypeConfiguration 继承的 Configuration 类即可:

public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
  // place any further configuration rules in the constructor
}

在 DataContext 的 OnModelCreating 方法中,添加配置:

modelBuilder.Configurations.Add(new ContactConfiguration()); modelBuilder.Configurations.Add(new EmployeeConfiguration());

假设在此实例中采用 TPT 方法,将专用版本 Employee 映射到其自己的表,并与联系人具有 FK 关系:

 modelBuilder.Entity<Contact>().Map<Employee>(m => m.ToTable("Employees"));

至少对我来说,这会创建一个不带“DoNotMap”属性的联系人表和一个不带“DoNotMap”表且与联系人具有 PK/FK 关系的 Employee 表。尽管我没有从 EntityBaseConfiguration 继承 Employee Configuration,但它仍然以某种方式选择 Ignore 并将其排除。 我假设如果我们采用 TPH 方法,我们最终只会得到一个联系人表和鉴别器列。 TPC 显然会重新创建 Employee 表中的所有 Contact 属性,但我不确定它是否也会创建 DoNotMap 属性 - 将在我有空的时候进行测试。

简而言之,回到最初的问题,我不确定为什么 NotMapped 属性和 Ignore 都会发生这种情况。当我的 EmployeeConfiguration 继承自 EntityBaseConfiguration 时,我收到错误。我不热衷于解决方法,因为首先,错误在其声明中是错误的,其次,这是一个很容易再次陷入的错误,即使解决方案现在非常简单。

希望对那些在继承问题上遇到困难的人有所帮助。

问候

关于c# - EF5 和 .NET 4.5 中的 NotMapped 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12362243/

相关文章:

javascript - 如何使用 javascript 将小时转换为 12 HRS/24 HRS 格式

c# - 在C#中播放声音

c# - 将相关数据添加到 Entity Framework 5 中的交叉引用表(代码优先)

view - 从 EF Core 3.1 通过 Code First 创建 View

c++ - 在 Linux 上测试的工作代码在 win 7、Visual Studio 12 上抛出 'error C3646'

c# - 如何使用 C# 控制 Windows 中的任务栏

c# - 带有 List<object> 的 JSON.NET 的 System.OutOfMemoryException

c# - 属性 'DepartmentID' 不能配置为导航属性

visual-studio-2012 - 安装 Window Azure 存储包时遇到问题

c++ - 优化 Microsoft Visual Studio 编译器生成的程序集