简体   繁体   中英

EF6 Mapping base class properties and derived with different tables

I'm using Entity Framework 6 and I have the following base class

public class ModelBase { public int ModelBaseId { get; set; } public string Name { get; set; } public string Description { get; set; } public IList<Notification> Notifications { get; set; } public IList<Assignable> Viewers { get; set; } }

and a derived

public class Model : ModelBase { public int ModelId { get; set; } public User Owner { get; set; } public DateTime Date { get; set; } public string Observations { get; set; } public User Assignament { get; set; } }

The ModelBase properties are populated with the same data for all derived class instances. I want to map the base entity properties with a table(A) and properties of the derived class with another table(B). And when I add Model just insert in the table B. Is there any way to do this?? Is something complex to explain but I've tried to simplify my problem.

Thanks in advance!

Assuming you are using Codefirst... You should try mapping them..

public class ModelBaseMap:EntityTypeConfiguration<ModelBase>
{
    public ModelBaseMap()
    {
        this.toTable("ModelBase");
    }
}

public class ModelMap:EntityTypeConfiguration<Model>
{
    public ModelBaseMap()
    {
        this.toTable("Model");
    }
}

I think it should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM