EF核心相关实体的多对多更新

时间:2019-02-28 13:45:48

标签: .net many-to-many entity-framework-core insert-update ef-core-2.0

我有以下dbContext:

class MyDbContext: DbContext
{
    public DbSet<First> Firsts { get; set; }
    public DbSet<Second> Seconds { get; set; }
    public DbSet<FirstSecond> FirstSeconds { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<First>().HasMany(f => f.FirstSeconds).WithOne(fs => fs.First);
        modelBuilder.Entity<First>().Property(prop => prop.RowVersion).IsRowVersion();

        modelBuilder.Entity<Second>().HasMany(s => s.FirstSeconds).WithOne(fs => fs.Second);
        modelBuilder.Entity<Second>().Property(prop => prop.RowVersion).IsRowVersion();

        modelBuilder.Entity<FirstSecond>(e => e.HasKey(k => new
        {
            k.FirstId, k.SecondId
        }));
        modelBuilder.Entity<FirstSecond>().Property(prop => prop.RowVersion).IsRowVersion();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=localhost;Database=myDataBase;Trusted_Connection=True;");
        base.OnConfiguring(optionsBuilder);
    }
}

public class First
{
    public Guid Id { get; set; }
    public virtual Collection<FirstSecond> FirstSeconds { get; set; }
    public byte[] RowVersion { get; set; }
}

public class Second
{
    public Guid Id { get; set; }
    public virtual Collection<FirstSecond> FirstSeconds { get; set; }
    public byte[] RowVersion { get; set; }
}

public class FirstSecond
{
    public Guid FirstId { get; set; }
    public Guid SecondId { get; set; }
    public virtual First First { get; set; }
    public virtual Second Second { get; set; }
    public byte[] RowVersion { get; set; }
}

}

我有四个头等舱实例。然后创建类Second的实例,然后在同一个dbContext中更新此实例,以替换在创建的Second实例中First类的第三个实例:

            using (var dbContext = new MyDbContext())
        {
            var firstInstances = dbContext.Firsts.ToList();
            var secondInstanceToCreate = new Second()
            {
                Id = Guid.NewGuid(),
                FirstSeconds = new Collection<FirstSecond>()
                {
                    new FirstSecond()
                    {
                        FirstId = firstInstances[0].Id,
                    },
                    new FirstSecond()
                    {
                        FirstId = firstInstances[1].Id
                    },
                    new FirstSecond()
                    {
                        FirstId = firstInstances[2].Id,
                    },
                }
            };

            dbContext.Seconds.Add(secondInstanceToCreate);
            dbContext.SaveChanges();

            obj = dbContext.Seconds.Include(s => s.FirstSeconds).ThenInclude(ss => ss.First)
                .First(e => e.Id == secondInstanceToCreate.Id);

            var newCollection = new Collection<FirstSecond>()
            {
                obj.FirstSeconds[0],
                obj.FirstSeconds[1],
                new FirstSecond()
                {
                    FirstId = firstInstances[3].Id,
                }
            };

            obj.FirstSeconds = newCollection;

            dbContext.Update(obj);
            dbContext.SaveChanges();
        }

在这种情况下,在第二类实例中,我将具有预期的行为: 在更新第二个实例之前,我有第一个1,2,3实例。更新后,我将拥有1,2,4。

second:{
firsts: [1,2,3]
}
//update operation performed
second:{
firsts:[1,2,4]
}

问题是:为什么如果我将在单独的dbContext中执行Update操作,我将拥有First 1,2,3,4而不是1,2,4的实例?

//after update operation
second:{
firsts: [1,2,3,4]
}

0 个答案:

没有答案