EF多对多导致重复

时间:2017-04-25 09:40:02

标签: c# entity-framework

我遇到多对多关系的问题,因为它会导致重复记录。

我有这些课程:

class Flyer { Id, virtual ICollection<FlyerPage> Pages }

class FlyerPage { Id, FlyerId, virtual Flyer Flyer, ICollection<FlyerPageKeyword> Keywords }

并且

class FlyerPageKeyword { Id, Key, virtual ICollection<FlyerPage> Pages }

所以FlyerPageFlyerPageKeyword是多对多的。它工作,记录保存到DB。

但它也增加了重复的关键字。

我的背景:

public class Context : DbContext
{
    public Context() : base("name=DbConnectionString")
    {
        Database.SetInitializer(new CreateDatabaseIfNotExists<Context>());
    }

    public virtual DbSet<Flyer> Flyers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Configurations.Add(new FlyersEntityConfiguration());
        modelBuilder.Configurations.Add(new FlyersPageEntityConfiguration());
        modelBuilder.Configurations.Add(new FlyersPageKeywordsEntityConfiguration());
    }
}



public class FlyersEntityConfiguration : EntityTypeConfiguration<Flyer>
{
    public FlyersEntityConfiguration()
    {
        this.Map(flyer => flyer.ToTable("Flyer", "Flyers"));

        this.HasMany(flyer => flyer.Pages)
            .WithRequired(page => page.Flyer)
            .HasForeignKey(page => page.FlyerId);
    }
}

public class FlyersPageEntityConfiguration : EntityTypeConfiguration<FlyerPage>
{
    public FlyersPageEntityConfiguration()
    {
        this.Map(pages => pages.ToTable("Page", "Flyers"));
        this.HasMany(page => page.Keywords).WithMany(keywords => keywords.FlyerPages).Map(cfg =>
        {
            cfg.ToTable("PageKeywords", "Flyers.Page");
            cfg.MapLeftKey("FlyerPageId");
            cfg.MapRightKey("KeywordId");
        });
    }
}
public class FlyersPageKeywordsEntityConfiguration : EntityTypeConfiguration<FlyerPageKeyword>
{
    public FlyersPageKeywordsEntityConfiguration()
    {
        this.ToTable("Keyword", "Flyers.Page");
    }
}

Flyer是聚合根,所以我对它进行操作。无法仅保存关键字。

var context = new Context();
        var flyer = new Flyer
        {
            Pages = new List<FlyerPage>
            {
                new FlyerPage
                {
                    Id = Guid.NewGuid(),
                    Keywords = new List<FlyerPageKeyword> {new FlyerPageKeyword {Key = "One"}}
                },
                new FlyerPage
                {
                    Keywords = new List<FlyerPageKeyword> {new FlyerPageKeyword {Key = "Two"}}
                }
            }
        };
        context.Flyers.Add(flyer);
        context.SaveChanges();

2 个答案:

答案 0 :(得分:2)

基本上是在创建新关键字条目之前使用现有关键字条目:

someKeyword = context.Keywords.FirstOrDefault(x => x.Key == "One") ?? new FlyerPageKeyword {Key = "One"};

如果您只是new已存在的关键字,则会获得重复项。

答案 1 :(得分:1)

为避免重复,您的传单分配应如下

var context = new Context();
        var flyer = new Flyer
        {
            Pages = new List<FlyerPage>
            {
                new FlyerPage
                {
                    Id = Guid.NewGuid(),
                    Keywords = new List<FlyerPageKeyword>
                        {
                          context.Keywords.FirstOrDefault(x => x.Key == "One") ?? new FlyerPageKeyword {Key = "One"}
                        }


                },
                new FlyerPage
                {
                    Keywords = new List<FlyerPageKeyword> 
                         {
                          context.Keywords.FirstOrDefault(x => x.Key == "Two")?? new FlyerPageKeyword {Key = "Two"}
                         }
                }
            }
        };
        context.Flyers.Add(flyer);
        context.SaveChanges();