多个实体到同一个DbSet [英] Multiple entities to same DbSet

查看:41
本文介绍了多个实体到同一个DbSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说我有两个不同的班级.它们共享一些属性,但也有一些单独的属性.

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; }
}

现在的诀窍是,我想将这两种类型存储为同一实体.如果我将单个属性转换为JSON,则有可能.所以我可以建立一个共享实体,像这样:

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; }
}

但是,如果这样做,我将失去静态类型语言的所有优点以及多态性的所有优点,例如,这样做(例如 A类可能与B类).

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 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();
    }
}

更多信息此处.

这篇关于多个实体到同一个DbSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆