Entity Framework code first database for one to one to many association
Entity Framework code first database for one to one to many association
我正在使用 entityframework 首先使用代码构建数据库。目的是通过 webservice 调用同步远程数据库。我已经为网络服务调用功能构建了一个结构化实体。
我需要如何对 dB 上下文进行编码才能创建以下关联?
cart.cs通过AssociationsCartAux给出cart_row,如下
public class order
{
[Key]
public long? id { get; set; }
public long? id_cart { get; set; } //which is a foreign key
public string invoice_number { get; set; }
....
}
public class cart
{
public long? id { get; set; }
...
public AssociationsCartAux associations { get; set; }
}
public class AssociationsCartAux : PrestaShopEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long? id_virtual { get; set; }
public List<cart_row> cart_rows { get; set; }
public AssociationsCartAux()
{
this.cart_rows = new List<cart_row>();
}
}
public class cart_row
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long? id_cartRow_fictive { get; set; }
public long? id_product { get; set; }
public int quantity { get; set; }
....
}
我在我的 dbcontext 文件中尝试了以下代码
public DbSet<cart> cart { get; set; }
public DbSet<cart_row> cart_row { get; set; }
public DbSet<order> order { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<AssociationsCartAux>()
.HasMany(p => p.cart_rows)
.WithMany().Map(c =>
{
c.ToTable("cart_assocation");
});
当我 运行 代码时,它正在构建下表
dbo.carts
dbo.cart_row
dbo.AssociationsCartAuxes
- id_virtual
dbo.cart_assocation <=== 购物车与 CART_ROW
之间的关联
AssociationsCartAux_id_virtual
cart_row_id_cartRow_fictive
当我重新加载数据同步时 ==> 没问题
当我通过订单更新重新加载数据同步时 ==>
dbo.AssociationsCartAuxes 尽管更新了当前数据并仅添加了新数据,但现有数据的体积增加了一倍。涉及cart和cart_row之间的linq关联丢失。这让我假设我没有正确构建我的 dbcontext。
我不熟悉 entity framework 所以我尝试了一些方法来做到这一点将 AssociationsCartAux.cs 作为 ComplexType 但由于 cart_row 对象在里面。
提前感谢您的帮助。
有关其他信息,我将如何将数据放入表中
foreach (cart panier in listcart)
{
var result = ctx.cart.SingleOrDefault(b => b.id == panier.id);
if (result == null)
{
ctx.cart.Add(panier);
ctx.SaveChanges();
}
}
foreach (order commande in listorder)
{
var result = ctx.order.SingleOrDefault(b => b.id == commande.id);
if (result == null)
{
ctx.order.Add(commande);
ctx.SaveChanges();
}
}
一对多关系不需要辅助table,用:
就够了
public class cart
{
public long? id { get; set; }
...
public List<cart_row> cart_rows { get; set; }
}
而 EF 的魔力将完成剩下的工作,尽管在另一端显式定义关系也是一个好主意:
public class cart_row
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long? id_cartRow_fictive { get; set; }
public long? id_product { get; set; }
public int quantity { get; set; }
....
public long? id_cart {get; set;}
public cart cart {get; set;}
}
并且在 DbContext 中:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<cart_row>()
.HasOne(cr => cr.cart)
.WithMany(c => c.cart_rows)
.HasForeignKey(cr => cr.id_cart);
}
但是还有许多其他方法可以实现此目的,有关更多信息,请查看:
https://docs.microsoft.com/en-us/ef/core/modeling/relationships
我找到了答案,也许它可以帮助某人建立这样的三级关联。只需添加:
modelBuilder.Entity().ToTable("carts");
到数据库上下文如下。
在数据库上下文中:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<cart>()
.HasRequired(s => s.associations)
.WithRequiredPrincipal(ad => ad.cart);
modelBuilder.Entity<cart_row>()
.HasRequired(cr => cr.AssociationsCartAux)
.WithMany(c => c.cart_rows)
.HasForeignKey(cr => cr.id_AssocCartAux);
modelBuilder.Entity<AssociationsCartAux>().ToTable("carts"); <== this solved my issue
我正在使用 entityframework 首先使用代码构建数据库。目的是通过 webservice 调用同步远程数据库。我已经为网络服务调用功能构建了一个结构化实体。
我需要如何对 dB 上下文进行编码才能创建以下关联?
cart.cs通过AssociationsCartAux给出cart_row,如下
public class order
{
[Key]
public long? id { get; set; }
public long? id_cart { get; set; } //which is a foreign key
public string invoice_number { get; set; }
....
}
public class cart
{
public long? id { get; set; }
...
public AssociationsCartAux associations { get; set; }
}
public class AssociationsCartAux : PrestaShopEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long? id_virtual { get; set; }
public List<cart_row> cart_rows { get; set; }
public AssociationsCartAux()
{
this.cart_rows = new List<cart_row>();
}
}
public class cart_row
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long? id_cartRow_fictive { get; set; }
public long? id_product { get; set; }
public int quantity { get; set; }
....
}
我在我的 dbcontext 文件中尝试了以下代码
public DbSet<cart> cart { get; set; }
public DbSet<cart_row> cart_row { get; set; }
public DbSet<order> order { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<AssociationsCartAux>()
.HasMany(p => p.cart_rows)
.WithMany().Map(c =>
{
c.ToTable("cart_assocation");
});
当我 运行 代码时,它正在构建下表
dbo.carts
dbo.cart_row
dbo.AssociationsCartAuxes
- id_virtual
dbo.cart_assocation <=== 购物车与 CART_ROW
之间的关联AssociationsCartAux_id_virtual
cart_row_id_cartRow_fictive
当我重新加载数据同步时 ==> 没问题 当我通过订单更新重新加载数据同步时 ==> dbo.AssociationsCartAuxes 尽管更新了当前数据并仅添加了新数据,但现有数据的体积增加了一倍。涉及cart和cart_row之间的linq关联丢失。这让我假设我没有正确构建我的 dbcontext。
我不熟悉 entity framework 所以我尝试了一些方法来做到这一点将 AssociationsCartAux.cs 作为 ComplexType 但由于 cart_row 对象在里面。
提前感谢您的帮助。
有关其他信息,我将如何将数据放入表中
foreach (cart panier in listcart)
{
var result = ctx.cart.SingleOrDefault(b => b.id == panier.id);
if (result == null)
{
ctx.cart.Add(panier);
ctx.SaveChanges();
}
}
foreach (order commande in listorder)
{
var result = ctx.order.SingleOrDefault(b => b.id == commande.id);
if (result == null)
{
ctx.order.Add(commande);
ctx.SaveChanges();
}
}
一对多关系不需要辅助table,用:
就够了public class cart
{
public long? id { get; set; }
...
public List<cart_row> cart_rows { get; set; }
}
而 EF 的魔力将完成剩下的工作,尽管在另一端显式定义关系也是一个好主意:
public class cart_row
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long? id_cartRow_fictive { get; set; }
public long? id_product { get; set; }
public int quantity { get; set; }
....
public long? id_cart {get; set;}
public cart cart {get; set;}
}
并且在 DbContext 中:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<cart_row>()
.HasOne(cr => cr.cart)
.WithMany(c => c.cart_rows)
.HasForeignKey(cr => cr.id_cart);
}
但是还有许多其他方法可以实现此目的,有关更多信息,请查看: https://docs.microsoft.com/en-us/ef/core/modeling/relationships
我找到了答案,也许它可以帮助某人建立这样的三级关联。只需添加: modelBuilder.Entity().ToTable("carts");
到数据库上下文如下。
在数据库上下文中:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<cart>()
.HasRequired(s => s.associations)
.WithRequiredPrincipal(ad => ad.cart);
modelBuilder.Entity<cart_row>()
.HasRequired(cr => cr.AssociationsCartAux)
.WithMany(c => c.cart_rows)
.HasForeignKey(cr => cr.id_AssocCartAux);
modelBuilder.Entity<AssociationsCartAux>().ToTable("carts"); <== this solved my issue