Entity data model with Master-detail scenario [modified]
-
hi, I am using entity data model and i have master-detail scenario.(Patient-PatientAddresses) Everything is going on fine if i am updating any data either in master table or detail table. But when i am going to insert a new detail record while updating master entity, it gives me following error : "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." I googled so much, but not getting proper idea. Here is my code :
using (var context = new Entities())
{
try
{
var record = (from p in context.Patient.Include("PatientAddress")
where p.PatientId == patient.PatientId
select p).FirstOrDefault();if (record == null) { context.AddToPatient(patient); } else { foreach (var item in patient.PatientAddress) { if (item.EntityKey == null) { context.AddToPatientAddress(item); // <--Throws Exception here. continue; } context.ApplyPropertyChanges("PatientAddress", item); } context.ApplyPropertyChanges("Patient", patient); } context.SaveChanges(); return true; } catch { return false; } }
Any suggessions is appreciated.
modified on Wednesday, February 3, 2010 9:55 AM
-
hi, I am using entity data model and i have master-detail scenario.(Patient-PatientAddresses) Everything is going on fine if i am updating any data either in master table or detail table. But when i am going to insert a new detail record while updating master entity, it gives me following error : "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." I googled so much, but not getting proper idea. Here is my code :
using (var context = new Entities())
{
try
{
var record = (from p in context.Patient.Include("PatientAddress")
where p.PatientId == patient.PatientId
select p).FirstOrDefault();if (record == null) { context.AddToPatient(patient); } else { foreach (var item in patient.PatientAddress) { if (item.EntityKey == null) { context.AddToPatientAddress(item); // <--Throws Exception here. continue; } context.ApplyPropertyChanges("PatientAddress", item); } context.ApplyPropertyChanges("Patient", patient); } context.SaveChanges(); return true; } catch { return false; } }
Any suggessions is appreciated.
modified on Wednesday, February 3, 2010 9:55 AM
Fix your post. The code is unreadable.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Fix your post. The code is unreadable.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
hi, I am using entity data model and i have master-detail scenario.(Patient-PatientAddresses) Everything is going on fine if i am updating any data either in master table or detail table. But when i am going to insert a new detail record while updating master entity, it gives me following error : "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." I googled so much, but not getting proper idea. Here is my code :
using (var context = new Entities())
{
try
{
var record = (from p in context.Patient.Include("PatientAddress")
where p.PatientId == patient.PatientId
select p).FirstOrDefault();if (record == null) { context.AddToPatient(patient); } else { foreach (var item in patient.PatientAddress) { if (item.EntityKey == null) { context.AddToPatientAddress(item); // <--Throws Exception here. continue; } context.ApplyPropertyChanges("PatientAddress", item); } context.ApplyPropertyChanges("Patient", patient); } context.SaveChanges(); return true; } catch { return false; } }
Any suggessions is appreciated.
modified on Wednesday, February 3, 2010 9:55 AM
The exception tells you everything you need to know. If you can't find any useful information then you need to learn how to search better. http://www.bing.com/search?q=An+object+with+the+same+key+already+exists+in+the+ObjectStateManager&src=IE-SearchBox&FORM=IE8SRC[^]
I know the language. I've read a book. - _Madmatt
-
Sorry but i can read it from here. Can you tell me which lines of the code you are not able to read?
It's OK now. The first time I looked at it, the entire code snippet was on a single line and you had to scroll left and right to read it all.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
The exception tells you everything you need to know. If you can't find any useful information then you need to learn how to search better. http://www.bing.com/search?q=An+object+with+the+same+key+already+exists+in+the+ObjectStateManager&src=IE-SearchBox&FORM=IE8SRC[^]
I know the language. I've read a book. - _Madmatt