简体   繁体   中英

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.

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

Entity class:

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

Company Class (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)

 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

Obviously the typeof doesnt compile

 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.

Unfortunately, I don't think you'll be able to use EF to automatically create that kind of relationship. 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()));
    }
}

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