• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在 Entity Framework Code First 忽略基类型是否也会忽略子类?

用户头像
it1352
帮助6

问题说明

我正在尝试模拟一个场景,在该场景中我从 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 以显式排除来自模型的属性或类型.

The type 'EfInheritanceTest.Models.Order' and the type 'EfInheritanceTest.Models.Base.Order' both have the same simple name of 'Order' and so cannot be used in the same model. All types in a given model must have unique simple names. Use 'NotMappedAttribute' or call Ignore in the Code First fluent API to explicitly exclude a property or type from the model.

据我了解,在 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 数据注释.验证类型是定义为一个类,不是原始的或泛型的,并且不继承来自实体对象.

The type 'EfInheritanceTest.Models.Order' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive or generic, and does not inherit from EntityObject.

... 这似乎表明通过忽略基类,我也忽略了任何子类.完整代码如下.有什么办法可以解决这个问题并忽略"子类?或者这是 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");
                                                    });

        }
    }
}

正确答案

#1

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

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 继承映射.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tangckchg
系列文章
更多 icon
同类精品
更多 icon
继续加载