entity framework - EF Inserting Duplicate Parent Objects -


i have 2 classes:

public class foo {     public int fooid {get;set;}     public virtual icollection<bar> bars {get;set;} }  public class bar {     public int barid {get;set;}     public virtual foo {get;set;} } 

if run following code, foreign key conflict on fooid.

var foo = f in context.foos           f.fooid == 1           select f;  var bar = new bar(); bar.foo = foo;  context.bars.add(bar); context.savechanges(); 

if disable key checks in sql, end duplicate foo in database.

loading foo same context adding new bar related foo won't cause duplication. guess real code uses 2 different contexts.

the thing change in code (which won't compile because foo iqueryable<foo> , not foo) materialize foo, example:

var foo = (from f in context.foos           f.fooid == 1           select f).single(); 

other code snippet fine.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -