简体   繁体   English

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

[英]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

  • id_virtual id_virtual

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.

相关问题 使用代码优先实体框架执行一对多关联 - Perform an Association from one to many with code first entity framework 如何使用Entity Framework 4.1 Code First强制数据库中一对多关系的一对一关系 - How to use Entity Framework 4.1 Code First to force a one to one relationship for a one to many relationship in the database Entity Framework 5.0代码首先是一对一和一对多的关系 - Entity Framework 5.0 code first one to one and one to many relationship 实体框架6首先是一对一而不是一对多的代码 - Entity framework 6 code first one to one instead of one to many 实体框架代码一个实体的“多对多” - Entity Framework Code First many to many for one entity 代码优先实体框架核心中的一对多一对一 - One to Many with One Main in Code-First Entity Framework Core 在实体框架中使用关联表创建一对多 - Creating a one to many with an association table in Entity Framework 实体框架单向关联从一对多 - entity framework unidirectional association from one to many 实体框架协会错误一对多 - Entity Framework Association Error One to Many 实体框架6代码优先 - 通过注释多对多的方式 - Entity framework 6 code first - one way many to many via annotations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM