[英]Entity Framework code first database for one to one to many association
I'm building a database with entityframework using code first. 我正在使用代码构建一个使用entityframework的数据库。 The aim is to syncronize a remote database with webservice call. 目的是使远程数据库与webservice调用同步。 I already have a structured entities builded for webservice call function. 我已经为webservice调用函数构建了一个结构化实体。
How do i need to code the dB context for create the following association ? 我如何编码dB上下文以创建以下关联?
the cart.cs gives cart_row through AssociationsCartAux, as follow 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; }
....
}
I tried the following code in my dbcontext file 我在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");
});
When i run the code, it's building the following tables 当我运行代码时,它正在构建以下表格
dbo.carts dbo.carts
dbo.cart_row dbo.cart_row
dbo.AssociationsCartAuxes dbo.AssociationsCartAuxes
dbo.cart_assocation <=== ASSOCIATION BETWEEN CART AND CART_ROW dbo.cart_assocation <=== CART和CART_ROW之间的关联
AssociationsCartAux_id_virtual AssociationsCartAux_id_virtual
cart_row_id_cartRow_fictive cart_row_id_cartRow_fictive
when i reload the data syncronisation ==> no problem when i reload the data syncronisation with an order update ==> the dbo.AssociationsCartAuxes double in volume additionnaly to the existing data, despite of update the current data and add only the new one. 当我重新加载数据同步==>没有问题,当我重新加载数据同步与订单更新==> dbo.AssociationsCartAuxes的数量加倍现有数据,尽管更新当前数据并只添加新数据。 Involving the lost of the linq association between cart and cart_row. 涉及购物车和cart_row之间丢失linq关联。 Which let me suppose that i don't have correctly build my dbcontext. 这让我想我没有正确构建我的dbcontext。
I'm not familiar with entity framework so i tried some way to do this with putting AssociationsCartAux.cs as a ComplexType but not work because of cart_row object inside. 我不熟悉实体框架,所以我尝试了一些方法,将AssociationsCartAux.cs作为ComplexType放入,但因为cart_row对象里面没有工作。
Thanks you in advance for any help. 提前感谢您的帮助。
For additionnal information, i put the way how i put data into the tables 对于其他信息,我提出了如何将数据放入表格的方式
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();
}
}
You don't need an auxiliar table for a one-to-many relationship, it's enough with: 对于一对多的关系,您不需要辅助表,这对于:
public class cart
{
public long? id { get; set; }
...
public List<cart_row> cart_rows { get; set; }
}
And the magic of EF will do the rest, although it's a good idea to explicitly define the relationship in the other end as well: 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;}
}
And in the DbContext: 在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);
}
But there are many other ways in which you can achieve this, for more information check: https://docs.microsoft.com/en-us/ef/core/modeling/relationships 但是有很多其他方法可以实现这一点,有关更多信息,请访问: https : //docs.microsoft.com/en-us/ef/core/modeling/relationships
I found an answer, may be it could help someone to make a such third level association. 我找到了答案,也许可以帮助某人建立这样的第三级协会。 Simply add : modelBuilder.Entity().ToTable("carts"); 只需添加:modelBuilder.Entity()。ToTable(“carts”);
to the db context as follow. 到db上下文如下。
in the db context : 在db上下文中:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.