Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架核心不跟踪ICollection中的更改_C#_Entity Framework_.net Core - Fatal编程技术网

C# 实体框架核心不跟踪ICollection中的更改

C# 实体框架核心不跟踪ICollection中的更改,c#,entity-framework,.net-core,C#,Entity Framework,.net Core,我有两门课: public class TestProduct { public ICollection<Tester> Collection { get; set; } } public class Tester { public TestProduct TestProduct { get; set; } } 公共类测试产品 { 公共ICollection集合{get;set;} } 公共类测试员 { 公共TestProduct TestProduct{get;

我有两门课:

public class TestProduct
{
    public ICollection<Tester> Collection { get; set; }
}

public class Tester
{
    public TestProduct TestProduct { get; set; }
}
公共类测试产品
{
公共ICollection集合{get;set;}
}
公共类测试员
{
公共TestProduct TestProduct{get;set;}
}
我想跟踪对集合的更改,当您向集合添加内容时,我想跟踪它。但是,在下面的代码中,集合所在的TestProduct对象永远不会标记为已修改。如果我向其集合中添加了一些内容,则只有添加到集合中的对象才会标记为已修改。如何将TestProduct标记为已修改

public class BaseDbContext : DbContext
{
    public BaseDbContext(DbContextOptions<BaseDbContext> options) : base(options)
    {

    }

    public DbSet<TestProduct> TestProducts { get; set; }
    public DbSet<Tester> Testers { get; set; }

    public int SaveChangesForAudit()
    {
        foreach (var moddedEntity in ChangeTracker.Entries()
            .Where(p => p.State == EntityState.Modified))
        {
            var state = moddedEntity.State;
            // logic for doing something with state change, here I would expect TestProduct to show up, not Tester
        }
    }
}
公共类BaseDbContext:DbContext
{
public BaseDbContext(DbContextOptions选项):基本(选项)
{
}
公共数据库集测试产品{get;set;}
公共数据库集测试器{get;set;}
public int SaveChangesForAudit()
{
foreach(ChangeTracker.Entries()中的var moddedEntity)
.Where(p=>p.State==EntityState.Modified))
{
var state=moddedEntity.state;
//在这里,我希望TestProduct出现,而不是Tester
}
}
}
我正在运行的测试,仅供参考:

    [Fact]
    public void EditProduct_HandlesListChanges()
    {
        var testerId = Guid.NewGuid();
        using (var context = CreateContext("EditProduct_HandlesComplexProduct"))
        {
            context.TestProducts.Add(new TestProduct()
            {
                Created = DateTime.Now,
                Deleted = false,
                Id = Guid.NewGuid(),
                Name = "testproduct",
                TenantId = Guid.NewGuid(),
                Collection = new List<Tester>()
            });
            var tester = new Tester() {Id = testerId};
            context.Testers.Add(tester);
        }
        using (var context = CreateContext("EditProduct_HandlesComplexProduct"))
        {
            var prod = context.TestProducts.Include(tp => tp.Collection).Include(tp => tp.SubType).FirstOrDefault();
            var tester = context.Testers.First(x => x.Id == testerId);
            Assert.NotNull(prod);

            tester.TestProduct = prod;
            prod.Collection = new List<Tester>() { tester};

            context.SaveChangesForAudit(); //Here I will Audit changes
            Assert.Equal(1, prod.Versions.Count);

            context.Database.EnsureDeleted();
        }
    }

    private static InfrastructureTestDbContext CreateContext(string dbName)
    {
        var builder = new DbContextOptionsBuilder<BaseDbContext>();
        builder.UseInMemoryDatabase(dbName);
        return new InfrastructureTestDbContext(builder.Options);
    }
[事实]
public void EditProduct_handleListChanges()
{
var testerId=Guid.NewGuid();
使用(var context=CreateContext(“EditProduct\u HandlesComplexProduct”))
{
context.TestProducts.Add(新的TestProduct()
{
Created=DateTime。现在,
已删除=错误,
Id=Guid.NewGuid(),
Name=“testproduct”,
TenantId=Guid.NewGuid(),
集合=新列表()
});
var tester=new tester(){Id=testerId};
context.Testers.Add(tester);
}
使用(var context=CreateContext(“EditProduct\u HandlesComplexProduct”))
{
var prod=context.TestProducts.Include(tp=>tp.Collection).Include(tp=>tp.SubType).FirstOrDefault();
var tester=context.Testers.First(x=>x.Id==testerId);
Assert.NotNull(prod);
tester.TestProduct=prod;
产品集合=新列表(){tester};
context.SaveChangesForAudit();//这里我将审核更改
Assert.Equal(1,prod.Versions.Count);
context.Database.EnsureDeleted();
}
}
私有静态InfrastructureTestDbContext CreateContext(字符串dbName)
{
var builder=new DbContextOptionsBuilder();
builder.UseInMemoryDatabase(dbName);
返回新的InfrastructureTestDbContext(builder.Options);
}

我认为这就是问题所在

prod.Collection = new List<Tester>() { tester};

如果我没记错的话,你是在上下文对象释放后检查跟踪的实体吗?。因为“使用”将释放/完成对象。这可能是问题所在,我看不太清楚,但代码在savechangesforudit方法中运行,该方法在using中调用。我会解决这个问题。
prod.Collection.Clear();
prod.Collection.Add(tester);