c# - 实体框架代码第一个数据库,用于一对一关联
问题描述
我首先使用代码构建一个带有实体框架的数据库。目的是使远程数据库与 web 服务调用同步。我已经为 Web 服务调用功能构建了一个结构化实体。
我如何需要对 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_association <=== 购物车和购物车行之间的关联
关联CartAux_id_virtual
cart_row_id_cartRow_fictive
当我重新加载数据同步时 ==> 当我通过订单更新重新加载数据同步时没问题 ==> dbo.AssociationsCartAuxes 在现有数据的基础上增加一倍,尽管更新当前数据并仅添加新数据。涉及到 cart 和 cart_row 之间 linq 关联的丢失。这让我假设我没有正确构建我的 dbcontext。
我不熟悉实体框架,所以我尝试了一些方法来做到这一点,将 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();
}
}
解决方案
我找到了答案,也许它可以帮助某人建立这样的第三级关联。只需添加: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
推荐阅读
- elasticsearch - 弹性 - 在 inner_hits 中的嵌套对象上查询多个级别
- python - 如何获得从我的相机到 OpenCV ArUco Marker 的距离
- javascript - 错误:Route.get() 需要回调函数,但得到了 [object Undefined] Udemy Course Car Rental Website
- python - Flask,FlaskSocketIO - RuntimeError:无法从 WSGI 环境获取套接字
- c# - C# - Stream.Read 偏移工作不正确
- angular - 如何在不重新加载的情况下重新加载页面,让我们假设要显示的内容很大
- wordpress - get_the_permalink 通过自定义帖子 id 返回空。这以前一直在工作。但现在它不起作用
- reactjs - CKEditor5 React 自定义图片上传适配器不接受解析的 url
- javascript - 在已安装的依赖项中找不到全局
- hash - 用于“9X9XX99X9XX999999”形式的数字和字母字符串的良好哈希函数