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

c# - IMAP GMAIL getting folder list problem -

PHP proxy to get XML: problem with URL arguments -