实体框架代码优先的一对一关联数据库

时间:2019-04-07 18:12:01

标签: c# database entity-framework entity-framework-6

我首先使用代码构建带有entityframework的数据库。目的是通过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_assocation <===购物车和CART_ROW之间的关联

  • AssociationsCartAux_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();
                }
            }

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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