本文介绍了在实体框架迁移中如何种植具有多对多关系的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架迁移(在自动迁移模式下)。一切都很好,但我有一个问题:
当我有很多关系时,应该如何种数据。
例如我有两个模型类:

I use entity framework migration (in Automatic migration mode). Everything is Ok, but I have one question:How should I seed data when I have many to many relation.For example I have two model classes:

public class Parcel
{
  public int Id { get; set; }
  public string Description { get; set; }
  public double Weight { get; set; }
  public virtual ICollection<BuyingItem> Items { get; set; }
}

public class BuyingItem
{
    public int Id { get; set; }
    public decimal Price { get; set; }
    public virtual ICollection<Parcel> Parcels { get; set; }
}

我了解如何种子简单数据(对于PaymentSystem类)多少关系,但是我应该在Seed方法中写什么代码来生成一些Parcel和BuyingItem的实例?我的意思是使用DbContext.AddOrUpdate(),因为我不想在每次运行Update-Database时复制数据。

I understand how to seed simple data (for PaymentSystem class) and one-to-many relations, but what code should I write in Seed method to generate some instances of Parcel and BuyingItem? I mean using DbContext.AddOrUpdate(), because I don't want to duplicate data every time I run Update-Database.

protected override void Seed(ParcelDbContext context)
{
        context.AddOrUpdate(ps => ps.Id,
                            new PaymentSystem {Id = 1, Name = "Visa"},
                            new PaymentSystem {Id = 2, Name = "PayPal"},
                            new PaymentSystem {Id = 3, Name = "Cash"});
}







protected override void Seed(Context context)
{
    base.Seed(context);

    // This will create Parcel, BuyingItems and relations only once
    context.AddOrUpdate(new Parcel() 
    { 
        Id = 1, 
        Description = "Test", 
        Items = new List<BuyingItem>
        {
            new BuyingItem() { Id = 1, Price = 10M },
            new BuyingItem() { Id = 2, Price = 20M }
        }
    });

    context.SaveChanges();
}

是的。此代码创建Parcel,BuyingItems和关系,但如果我需要在其他Parcel中具有相同的BuyingItem(它们具有多对多关系),并且如果我对第二个包裹重复此代码,它将在数据库中重复BuyingItems(尽管我设置相同的Id)。示例:

Yes. This code create Parcel, BuyingItems and relation, but if I need the same BuyingItem in other Parcel (they have many-to-many relation) and if I repeat this code for the second parcel - it will duplicate BuyingItems in database (though I set the same Id's). Example:

protected override void Seed(Context context)
{
    base.Seed(context);

    context.AddOrUpdate(new Parcel() 
    { 
        Id = 1, 
        Description = "Test", 
        Items = new List<BuyingItem>
        {
            new BuyingItem() { Id = 1, Price = 10M },
            new BuyingItem() { Id = 2, Price = 20M }
        }
    });

    context.AddOrUpdate(new Parcel() 
    { 
        Id = 2, 
        Description = "Test2", 
        Items = new List<BuyingItem>
        {
            new BuyingItem() { Id = 1, Price = 10M },
            new BuyingItem() { Id = 2, Price = 20M }
        }
    });

    context.SaveChanges();
}

如何在不同的包裹中添加相同的BuyingItem?

How can I add same BuyingItems in different Parcels?

推荐答案

您必须以与在任何EF代码中建立多对多关系相同的方式填充多对多关系:

You must fill many-to-many relation in the same way as you build many-to-many relation in any EF code:

protected override void Seed(Context context)
{
    base.Seed(context);

    // This will create Parcel, BuyingItems and relations only once
    context.AddOrUpdate(new Parcel() 
    { 
        Id = 1, 
        Description = "Test", 
        Items = new List<BuyingItem>
        {
            new BuyingItem() { Id = 1, Price = 10M },
            new BuyingItem() { Id = 2, Price = 20M }
        }
    });

    context.SaveChanges();
}

指定 Id 将在数据库中使用至关重要,否则每个更新数据库将创建新记录。

Specifying Id which will be used in database is crucial otherwise each Update-Database will create new records.

AddOrUpdate 不支持以任何方式更改关系,因此您无法使用它来添加或删除下一次迁移中的关系。如果需要,您必须手动删除关系,通过加载 Parcel BuyingItems 并调用删除在导航收藏上添加以打破或添加新关系。

AddOrUpdate doesn't support changing relations in any way so you cannot use it to add or remove relations in next migration. If you need it you must manually remove relation by loading Parcel with BuyingItems and calling Remove or Add on navigation collection to break or add new relation.

这篇关于在实体框架迁移中如何种植具有多对多关系的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 21:49