Error returning to Entity Framework SaveChanges formula. AddObject Does Not Set Primary Key

I have a Property table and the other is a Detail table. When I use the disabled approach, when several properties are added, and then when we click the "Save" button, it saves the changes to the database. This approach is not saved for each added property, as the user can also remove it from the list.

That's what I'm doing

 foreach (Property P in Results) { if(P.PropertyId==0 && P.EntityState==EntityState.Added) Repository.Properties.AddObject(P); } 

but when i try to save data

 public void Save() { Repository.SaveChanges(); } 

he returns an error

Unable to determine the top end of "database.FK_Details_Property"
relations. Multiple objects added can have the same primary key.

I think this is because every time I call AddObject, its primary key is 0, and I do not know what could solve this problem. Does it look or sound familiar to everyone who met him?

Thanks in advance

+6
source share
4 answers

1) You can check the circular dependence. It can be between two Save Product objects with detailed objects or vice versa

Check here: http://social.msdn.microsoft.com/Forums/sk/adodotnetentityframework/thread/5ba666ff-0103-4a83-b4d0-743c16a99491

2) Be careful when adding Repository.Properties.AddObject (P); because the code behind the P object still has a link to the Detail table.

Ensure that the object associated with the parts has not yet saved the objects associated with it.

If a Detail object has 3 properties, when you save a Detail, you also save 3 properties.

0
source

its definitely identical column linked here.

Assuming you are using MS SQL please check what you paste on the table and any related

1) Identifier specification = YES

2) Is Identity = YES

3) Personality increment = 1

4) Seed of identity = 1

He is definitely trying to insert PK for you, so you need to look around. Make sure you also check child tables. If you still have problems and you are not using the first code method, try also restoring these tables in your EDMX.

+2
source

Just replace this code in the code:

 foreach (Property P in Results) { if(P.PropertyId==0 && P.EntityState==EntityState.Added) { var id = Repository.Properties.Max(p => (int?)p.PropertyId) ?? 0; P.PropertyId=++id; Repository.Properties.AddObject(P); Repository.SaveChanges(); } } 

of course, in the first line of the add file:

 using System.Linq; 

for multithreaded processing:

 foreach (Property P in Results) { if(P.PropertyId==0 && P.EntityState==EntityState.Added) { while(true){ try{ var id = Repository.Properties.Max(p => (int?)p.PropertyId) ?? 0; P.PropertyId=++id; Repository.Properties.AddObject(P); Repository.SaveChanges(); break; }catch{ continue; } } } } 

of course, first create a new instance of the 'property' object, then assign the field to the field variable, then call AddObject and SaveChanged!

0
source

A guide is good for creating a unique identifier without a database.

if you use int, you will need to make sure that you have “Store Generated” for your main key column in your edmx model.

0
source

Source: https://habr.com/ru/post/899527/


All Articles