Many-to-Many insert failing - Entity Framework 4.1 DbContext -


i using db first method, ef 4.1 dbcontext poco code gen.

my database has many-to-many relationship shown below:

employee

employeeid

employeename

account

accountid

accountname

employeeaccount

employeeid

accountid

the problem occurs when trying insert new employee, , assign them pre existing account, doing below:

employee emp = new employee(); emp.employeename = "test"; emp.accounts.add(methodthatlooksupaccountbyname("someaccountname"));  context.employees.add(emp); context.savechanges(); 

the sql executing (incorrectly), attempting insert new [account] record, , failing on constraint violation. of course, should not insert new [account] record, should insert new [employeeaccount] record, after inserting [employee].

any advice? thanks.

methodthatlooksupaccountbyname method return attached or detached object? in case, may try attach object returns context.

employee emp = new employee(); emp.employeename = "test"; var acc = methodthatlooksupaccountbyname("someaccountname"); context.attach(acc); //i don't remember if it's attach or attachobject, intellisense should there.  emp.accounts.add(acc);  context.employees.add(emp); context.savechanges(); 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -