EF Inserting Duplicate Parent Objects

前端 未结 4 1659
别跟我提以往
别跟我提以往 2021-01-13 02:29

I have two classes:

public class Foo
{
    public int FooId {get;set;}
    public virtual ICollection Bars {get;set;}
}

public class Bar
{
    pu         


        
4条回答
  •  孤城傲影
    2021-01-13 02:38

    Try

    var bar = new Bar();
    context.Bars.Add(bar);
    bar.Foo == foo;
    context.SaveChanges();
    

    It seems like the entity key isn't being set uniquely when assignment occurs before the entity is added to the context.

提交回复
热议问题