首页 > 解决方案 > 实体框架代码第一个数据库,用于一对一关联

问题描述

我首先使用代码构建一个带有实体框架的数据库。目的是使远程数据库与 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

dbo.cart_association <=== 购物车和购物车行之间的关联

当我重新加载数据同步时 ==> 当我通过订单更新重新加载数据同步时没问题 ==> 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();
                }
            }

标签: c#databaseentity-frameworkentity-framework-6

解决方案


我找到了答案,也许它可以帮助某人建立这样的第三级关联。只需添加: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

推荐阅读