简体   繁体   English

如何为复杂关系建立实体模型框架的种子

[英]How to seed this entity model framework for complex relationship

I have the following scenario. 我有以下情况。 We need to be able to fill forms for some tables, examples Companies (Empresa in Spanish), however we want the administrator to be able to extend the entity itself with additional fields. 我们需要能够填写一些表格的表格,例如公司(西班牙语中的Empresa),但是我们希望管理员能够使用其他字段来扩展实体本身。

I designed the following classes, and I need to seed at least one row, however its unclear to me how to seed one row of type CampoAdicional 我设计了以下课程,需要播种至少一排,但是我不清楚如何播种CampoAdicional类型的一排

Entity class: 实体类:

 public abstract class Entidad
    {
        [Key]
        public int Id { get; set; }
    }

Company Class (Empresas) 公司类别(Empresas)

 public class Empresa : Entidad
    {

        public string Nombre { get; set; }
        public string NIT { get; set; }
        public string NombreRepresentanteLegal { get; set; }
        public string TelefonoRepresentanteLegal { get; set; }
        public string NombreContacto { get; set; }
        public string TelefonoContacto { get; set; }

        public virtual ICollection<CampoAdicional> CamposAdicionales { get; set; }

    }

And the Additional Fields (Campo Adicional) 和其他字段(Campo Adicional)

 public class CampoAdicional
    {
        [Key]
        public int Id { get; set; }
        public string NombreCampo { get; set; }
        public virtual Tiposcampo TipoCampo { get; set; }

        public virtual Entidad Entidad { get; set; }
    }

However I dont know how to seed this class or table, because entity should be of subtype Company 但是我不知道如何为此类或表添加种子,因为实体应该是Company的子类型。

Obviously the typeof doesnt compile 显然,typeof无法编译

 context.CampoAdicionals.Add(new CampoAdicional() { Entidad = typeof(Empresa), Id = 1, NombreCampo = "TwitterHandle", TipoCampo = Tiposcampo.TextoUnaLinea });

Update 1: Please note that the additional fields are for the entire entity company not for each company. 更新1:请注意,其他字段适用于整个实体公司,而不适用于每个公司。

Unfortunately, I don't think you'll be able to use EF to automatically create that kind of relationship. 不幸的是,我认为您将无法使用EF自动创建这种关系。 You might be able to do something similar with special getters and such: 您也许可以使用特殊的吸气剂进行类似的操作,例如:

public class Entidad
{
    // stuff...

    public IEnumerable<CampoAdicional> CamposAdicionales 
    { 
       get { return CampoAdicional.GetAll(this); }
    }
}

public class CampoAdicional
{
    [Key]
    public int Id { get; set; }
    public string NombreCampo { get; set; }
    public virtual Tiposcampo TipoCampo { get; set; }

    protected string EntidadType { get; set; }

    // You will need some mapping between Type and the EntidadType string
    // that will be stored in the database. 
    // Maybe Type.FullName and Type.GetType(string)?
    protected Type MapEntidadTypeToType();
    protected string MapTypeToEntidadType(Type t);

    [NotMapped]
    public Type 
    {            
        get { return MapEntidadTypeToType(); }
        // maybe also check that Entidad.IsAssignableFrom(value) == true
        set { EntidadType = MapTypeToEntidadType(value); }
    }

    public static IEnumerable<CampoAdicional> GetAll(Entidad ent)
    {
        return context.CampoAdicionals
            .Where(a => a.EntidadType == MapTypeToEntidadType(ent.GetType()));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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