Does ignoring a base type in Entity Framework Code First also ignore subclasses?

你离开我真会死。 提交于 2019-12-20 05:34:08

问题


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:

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.

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:

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.

... 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:


I don't think that this will work if the child class has the same name as the parent class. I've definitely done this where the derived class has a different name than the parent class, but I doesn't look like this is possible when the names are the same (which I didn't know before). To test it, I took one of my projects where the inheritance was working with EF and I changed the names to fit the naming scheme that you have above and I'm getting the same errors that you list. It looks like this might be a limitation of the EF type mapping. Are you able to change the name of your derived class to be different than the parent?




回答2:


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

modelBuilder.Ignore<MyBaseClass>();

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

The second error was related to:

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

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

There is also an excellent post on EF inheritance mapping.



来源:https://stackoverflow.com/questions/24814875/does-ignoring-a-base-type-in-entity-framework-code-first-also-ignore-subclasses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!