0

我現在有在我的模型項目,RegistroItem和ItemTienda 3個實體的實體框架代碼優先:RegistroItem從項目繼承和ItemTienda有一個單與項目的多對多關係。我配置了RegistrosItems表使用這樣流利的API:實體B從實體A(使用MapInheritedProperties)打破了A和C的關係繼承 -

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

的問題是,當我把這些代碼,我的實體ItemTienda停止跟蹤與項目的關係,代碼第一次不加Foreing關鍵規則到SQL表ItemsTiendas。我已經嘗試了很多東西,但都沒有工作,你有什麼想法可能是什麼問題?這是實體的定義:

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

回答

0

在我看來,你應該讓這個變量爲「虛擬」。

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