实体框架代码第一个数据库用于一对一到多个关联

问题描述 投票:0回答:2

我正在使用代码构建一个使用entityframework的数据库。目的是使远程数据库与webservice调用同步。我已经为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和CART_ROW之间的关联

  • AssociationsCartAux_id_virtual
  • cart_row_id_cartRow_fictive

当我重新加载数据同步==>没有问题,当我重新加载数据同步与订单更新==> dbo.AssociationsCartAuxes的数量加倍现有数据,尽管更新当前数据并只添加新数据。涉及购物车和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# database entity-framework entity-framework-6
2个回答
0
投票

对于一对多的关系,您不需要辅助表,这对于:

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


0
投票

我找到了答案,也许可以帮助某人建立这样的第三级协会。只需添加:modelBuilder.Entity()。ToTable(“carts”);

到db上下文如下。

在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
© www.soinside.com 2019 - 2024. All rights reserved.