本文介绍了在 Entity Framework Code First 中忽略基类型是否也会忽略子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟一个场景,在该场景中我从 3rd 方库中的具体基类继承,然后使用 Entity Framework Code First 映射我自己的类.我真的希望我的类与基类具有相同的简单名称.我显然无法更改基类的类名,也无法将基类更改为抽象.正如预期的那样,我收到以下错误:

I'm attempting to simulate a scenario in which I am inheriting from concrete base classes in a 3rd party library, then mapping my own classes using Entity Framework Code First. I would really prefer for my classes to have the same simple name as the base classes. I obviously can't change the class names of the base classes, nor can I change the base class to abstract. As expected, I get the following error:

类型EfInheritanceTest.Models.Order"和类型'EfInheritanceTest.Models.Base.Order' 都具有相同的简单名称的订单",因此不能在同一模型中使用.一个中的所有类型给定模型必须具有唯一的简单名称.使用NotMappedAttribute"或在 Code First fluent API 中调用 Ignore 以显式排除来自模型的属性或类型.

据我了解,在 EF6 中,只要实际映射了其中一个类,这是可能的.但是,如果我尝试使用 fluent API 忽略基类,则会收到以下错误:

As I understand it, in EF6 this is possible so long as only one of the classes is actually mapped. However, if I attempt to ignore the base class using the fluent API, I get the following error instead:

未映射类型EfInheritanceTest.Models.Order".检查没有使用 Ignore 方法明确排除该类型或 NotMappedAttribute 数据注释.验证类型是定义为一个类,不是原始的或泛型的,并且不继承来自实体对象.

... 这似乎表明通过忽略基类,我也忽略了任何子类.完整代码如下.有什么办法可以解决这个问题并忽略"子类?或者这是 EF 类型映射的限制?

... which seems to indicate that by ignoring the base class, I ignore any subclasses as well. Full code below. Any way to work around this and "unignore" the subclass? Or is this a limitation of EF type mapping?

namespace EfInheritanceTest.Models.Base
{
    public class Order
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual decimal Amount { get; set; }
    }
}

namespace EfInheritanceTest.Models
{
    public class Order : Base.Order
    {
        public virtual DateTime OrderDate { get; set; }
    }
}

namespace EfInheritanceTest.Data
{
    public class OrdersDbContext : DbContext
    {
        public OrdersDbContext() : base ("OrdersDbContext") { }

        public IDbSet<EfInheritanceTest.Models.Order> Orders { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Types<Models.Base.Order>().Configure(c => c.Ignore());
            modelBuilder.Types<Models.Order>().Configure(c => c.ToTable("order"));
            modelBuilder.Entity<Models.Order>().Map(c =>
                                                    {
                                                        c.MapInheritedProperties();
                                                        c.ToTable("order");
                                                    });

        }
    }
}

推荐答案

反正有点晚了:我用非常简单的配置就解决了这个问题:

It's a bit late, anyway: I was able to solve the issue with very simple configuration:

modelBuilder.Ignore<MyBaseClass>();

所以 EF 根本不关心 MyBaseClass,而是与 MyInheritedClass 一起工作,因为它不是.完美!

So EF don't care about MyBaseClass at all, but works with MyInheritedClass as it is not. Perfect!

第二个错误与:

 modelBuilder.Types<Models.Base.Order>().Configure(c => c.Ignore());

因为您从 EF 映射中排除了这两个类(它从 Models.Base.Order 中排除了所有层次结构).

as you excluded both classes from EF mapping (it excludes all hierarchy from Models.Base.Order).

EF 继承映射.

这篇关于在 Entity Framework Code First 中忽略基类型是否也会忽略子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 00:24