0

I currently have 3 entities in my model Item, RegistroItem and ItemTienda: RegistroItem is inherited from Item and ItemTienda has a one-to-many relationship with Item. I configured the RegistrosItems Table using Fluent API like this:

protected override void OnModelCreating(DbModelBuilder MB) {
    MB.Entity<Item>().Map(m => { m.ToTable("Items"); })
        .Map<RegistroItem>(m => {
            m.ToTable("RegistroItems");
            m.MapInheritedProperties(); 
        });

The problem is that as soon as I put that code, my Entity ItemTienda stops tracking the relationship with Item and Code First doesn't add the Foreing key rule to the SQL table ItemsTiendas. I have tried lot of things but none of then work, Do you have any idea what may be the problem? This are the definition of the entities:

public class Item : IRegistrable { 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public EstadoRegistrable Estado { get; set; }
    public int ProductoID { get; set; }
    [ForeignKey("ProductoID")]
    public Producto ProductoObj { get; set; }
    [Required] [MaxLength(30)]
    public string Marca { get; set; }
    [ForeignKey("Marca")]
    public Marca MarcaObj { get; set; }
    [DecimalPrecision(9,3)]
    public decimal Tamaño { get; set; }
    [MaxLength(100)]
    public string Complemento { get; set; }
    [MaxLength(500)]
    public string Descripción { get; set; }
}

[Semilla(-2147483648)]
public class RegistroItem : Item, IRegistro {
    public int IDRegistrable { get; set; }
    public DateTime Modificado { get; set; }
    public int ModificadorID { get; set; }
    public AcciónRegistro Acción { get; set; }
    public FuenteDatos Fuente { get; set; }
    [MaxLength(1200)] 
    public string ValoresOriginales { get; set; }
}

 public class ItemTienda : IRegistrable { 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public EstadoRegistrable Estado { get; set; }
    public int TiendaID { get; set; }
    [ForeignKey("TiendaID")]
    public Tienda TiendaObj { get; set; }
    public int ItemID { get; set; }
    [ForeignKey("ItemID")]
    public Item ItemObj { get; set; }
    [StringLength(100)]
    public string DescripciónTirilla { get; set; }
    [StringLength(100)]
    public string DescripciónEstantería { get; set; }
    [StringLength(200)]
    public string DescripciónWeb { get; set; }
    [StringLength(20)]
    public string Código { get; set; }
    [DecimalPrecision(19, 4)]
    public decimal Precio { get; set; }
    public DateTime FechaPrecio { get; set; }
    public int? UbicaciónTiendaID { get; set; }
    [ForeignKey("UbicaciónTiendaID")]
    public UbicaciónTienda UbicaciónTiendaObj { get; set; }
    public Int16 SolicitudesRevisión { get; set; }
}
Desmond
  • 406
  • 5
  • 7

1 Answers1

0

In my opinion, you should make this variable "virtual".

public class Item : IRegistrable { 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public EstadoRegistrable Estado { get; set; }
    public int ProductoID { get; set; }
    [ForeignKey("ProductoID")]
    public virtual Producto ProductoObj { get; set; }
    [Required] [MaxLength(30)]
    public string Marca { get; set; }
    [ForeignKey("Marca")]
    public Marca MarcaObj { get; set; }
    [DecimalPrecision(9,3)]
    public decimal Tamaño { get; set; }
    [MaxLength(100)]
    public string Complemento { get; set; }
    [MaxLength(500)]
    public string Descripción { get; set; }
}