Multiple entities to same DbSet

旧城冷巷雨未停 提交于 2021-01-27 20:06:16

问题


Let's say I have two different classes. They share some properties, but also have some individual ones.

public class A
{
    // Shared properties
    public int Id { get; set; }
    public DateTime CreatedDtm { get; set; }
    public string CreatedBy { get; set; }

    // Individual properties
    public string PhoneNumber { get; set; }
    public string EmailAddress { get; set; }
}

public class B
{
    // Shared properties
    public int Id { get; set; }
    public DateTime CreatedDtm { get; set; }
    public string CreatedBy { get; set; }

    // Individual properties
    public string StreetAddress { get; set; }
    public string PostalCode{ get; set; }
    public string City{ get; set; }
}

Now the trick is, that I would like to store these two types as the same entity. This is possible, if I convert the individual properties to JSON. So I could make a shared entity, like this:

public class C
{
    // Shared properties
    public int Id { get; set; }
    public DateTime CreatedDtm { get; set; }
    public string CreatedBy { get; set; }

    // Individual properties
    public object IndividualProperties { get; set; }
}

But if I do this, I lose all the advantages of a static typed language as well as all the benefits of polymorphism, doing this (e.g. class A might have some different validations from class B).

Isn't there some way of doing the conversion, just before saving the entity, via Entity Framework (Core)? I could of course write the mapping myself, but I got a feeling that there should be a shortcut to this...

Basically, I want to do something like this (doesn't matter if it's an interface, abstract class or something completely different):

public void AddEntity(ISomeSharedInterface entity) 
{
    C sharedEntity = SomeMappingMethod(entity);
    db = new Context();
    db.SharedEntities.Add(sharedEntity);
    db.SaveChanges();
}

If you wonder about this design, it is for an automation process. The various sub-forms which needs to be filled during the process vary, but I would like to store all items in the same table.


回答1:


If you create an abstract base class, this should just work without any additional configuration:

public abstract class Base
{
    // Shared properties
    public int Id { get; set; }
    public DateTime CreatedDtm { get; set; }
    public string CreatedBy { get; set; }
}

public class A : Base
{
    // Individual properties
    public string PhoneNumber { get; set; }
    public string EmailAddress { get; set; }
}

public class B : Base
{
    // Individual properties
    public string StreetAddress { get; set; }
    public string PostalCode{ get; set; }
    public string City{ get; set; }
}

class Context : DbContext
{
    DbSet<Base> Bases { get; set; }
}

public void AddEntity(Base entity) 
{
    using (db = new Context())
    {
        db.Bases.Add(entity);
        db.SaveChanges();
    }
}

More info here.



来源:https://stackoverflow.com/questions/59628432/multiple-entities-to-same-dbset

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