C# 实体框架关系未更新

C# 实体框架关系未更新,c#,entity-framework,C#,Entity Framework,我遇到了一个问题,即通过更新实体不会更新与其相关的实体。下面是一个代码示例 public class Foo { public int Id { get; set; } public virtual ICollection<Bar> Bars { get; set; } public virtual string SomeString { get; set; } } public class Bar { public int Id { get; se

我遇到了一个问题,即通过更新实体不会更新与其相关的实体。下面是一个代码示例

public class Foo
{
    public int Id { get; set; }
    public virtual ICollection<Bar> Bars { get; set; }
    public virtual string SomeString { get; set; }
}

public class Bar
{
    public int Id { get; set; }
}

public class FooMapping : EntityTypeConfiguration<Foo>
{
    public FooMapping()
    {
        HasKey(f => f.Id);
        HasMany(f => f.Bars);
    }
 }


public class MyDb : DbContext
{
    public IDbSet<Foo> Foos { get; set; }
    public IDbSet<Bar> Bars { get; set; }

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

        modelBuilder.Configurations.Add(new FooMapping());
    }
}
这将正确更新SomeString值,但不会修改条形图对象

我尝试了以下几点:

public void Update(Foo foo)
{
    foreach(Bar bar in foo.Bars)
    {
        _myDb.Entry(bar).State = EntityState.Modified;
    }

    _myDb.Entry(foo).State = EntityState.Modified;
    _myDb.SaveChanges();
}
但这会给出一个
DbUpdateConcurrencyException
,其中包含以下信息

Store update, insert, or delete statement affected an unexpected
number of rows (0). 
Entities may have been modified or deleted since entities were loaded. Refresh 
ObjectStateManager entries.
如何在不必从数据库中选择的情况下更新这些条

谢谢

更新

我已经修改了Bar实体和映射

public class Bar
{
    public int Id { get; set; }
    public virtual Foo Foo { get; set; }
}

public class FooMapping : EntityTypeConfiguration<Foo>
{
    public FooMapping()
    {
        HasKey(f => f.Id);
        HasMany(f => f.Bars).WithRequired(b => b.Foo);
    }
 }

public class BarMapping : EntityTypeConfiguration<Foo>
{
    public BarMapping()
    {
        HasKey(b => b.Id);
        HasRequired(b => b.Foo);
    }
 }
公共类栏
{
公共int Id{get;set;}
公共虚拟Foo Foo{get;set;}
}
公共类FooMapping:EntityTypeConfiguration
{
公共地图()
{
HasKey(f=>f.Id);
有许多(f=>f.bar)。需要(b=>b.Foo);
}
}
公共类BarMapping:EntityTypeConfiguration
{
公共映射()
{
HasKey(b=>b.Id);
HasRequired(b=>b.Foo);
}
}

这不会改变任何事情。我应该提到的是,即使是使用
\u myDb.Foos.Add(foo)
的第一个版本也能正常工作(添加所有条)。但是,更新没有。

您可能会注意到,如果
Foo
有新的
Bar
项目,它们将被添加,并且(可能)现有项目将被取消。您可以尝试以下几种方法:

将Bar与Foo关联:

public class Bar
{
    public int Id { get; set; }

    public virtual Foo Foo { get; set; }
}

public class FooMapping : EntityTypeConfiguration<Foo>
{
    public FooMapping()
    {
        HasKey(f => f.Id);
        HasMany(f => f.Bars)
            .WithRequired(b => b.Foo); //check this syntax, I'm not entirely sure.
    }
}

让我知道它是否适合您。

尝试添加从Bar到Foo的关系。请注意,这是否会对您有所帮助,但我习惯于提供这两个导航属性。我的意思是:尝试将
FooId
和虚拟
Foo
添加到
Bar
类。第二个示例显然有效,但我希望不必先读取数据库,然后再更新它。第一种方法不走运,我知道。。。我昨天花了一整天的时间试图找到一个解决方案,几乎在这里发布了一个相同的问题,然后我找到了解决方案2。是的,我已经使用解决方案2有一段时间了,但感觉不对,尽管如此。我想要么是这样,要么是回到使用存储过程/Raw-SQL。方法2的另一个烦恼是所有属性都需要显式设置。因为这样做会导致无效操作异常
public class Bar
{
    public int Id { get; set; }

    public virtual Foo Foo { get; set; }
}

public class FooMapping : EntityTypeConfiguration<Foo>
{
    public FooMapping()
    {
        HasKey(f => f.Id);
        HasMany(f => f.Bars)
            .WithRequired(b => b.Foo); //check this syntax, I'm not entirely sure.
    }
}
public void Update(Foo foo)
{
    var existing = _myDb.Find(foo.Id);
    foreach(Bar bar in existing.Bars.ToList()) 
    {
        _myDb.Remove(bar);
    }
    existing.Bars.Clear();
    foreach(Bar bar in foo.Bars)
    {
        existing.Bars.Add(bar);
    }
    // map other properties...
    _myDb.SaveChanges();
}