c# - EF6 Updating 3 tables in one go -
i have database 3 tables
table customer
customerid (pk) name
table order
orderid (pk) customerid (fk) orderdate
table orderdetailline
orderdetaillineid orderid (fk) price productcode
what in entity framework add 1 customer, add several different orders, each many orderdetailline relating first customer inserted. have hooked relationship in sql server , imported them ef model. no problems. can insert 1 customer , many orders , orderdetailline first time records inserted ok correct databse tables
i looping around customer , orders in file adding adding customer , orders database
but when want add order same customer( getting primary key violation on customer customerid). ef trying re-insert same customer after initial context.savechanges();
so, how stop ef trying add same customer when using same datacontext?
i have been going around in circles hours , getting same error
{"the insert statement conflicted foreign key constraint \"fk_order_customer\". conflict occurred in database \"xxxxx\", table \"dbo.customer\", column 'customerid'
the customer newly created along order , orderlines. works first time , customerid ef. when add new order , orderlines after inital context.savechanges(); fk error
thanks taking time.
the problem was calling datacontext.savechanges() adding entities correct model. example,
customer.order.add(customerinfo);
well lack of understanding.
Comments
Post a Comment