6

I have a Property table and another is Detail table. Where I am using disconnected approach where multiple properties are added and then when we click the save button it will save changes to the database. This approach of not saving for every property added is because user can delete from the list also.

Here's what I am doing

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

but when I try to save the data

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

it returns error

Unable to determine the principal end of the 'database.FK_Details_Property'
relationship. Multiple added entities may have the same primary key.

I think its because every time I call AddObject its primary key is 0 and I don't know what could solve this problem. Is this similar or sounds familiar to anyone who has encountered it?

Thanks in advance

5
  • Have you set the the PropertyId to an Auto Generated value?
    – BigL
    Commented Oct 18, 2011 at 14:02
  • Ok just asking, i had forgotten it once. :) You could try to use AdtoTableName method of the repository or specify the table you wanna add the entity to.
    – BigL
    Commented Oct 18, 2011 at 14:54
  • I have also tried that. The Problem in this is there are multiple properties added and then if we try save changes it wont save because everybody will have Id=0; So is there any workaround to hold data with actual primary key or getting rid of this method. I have edited the code. Thanks.
    – user877127
    Commented Oct 18, 2011 at 16:22
  • No i dont have any circular dependency
    – user877127
    Commented Oct 18, 2011 at 20:30
  • Please add the code where you save Detail entity and the order you do the code with multiple property save. Commented Nov 5, 2011 at 8:52

4 Answers 4

2

its definitely identity column related here.

Assuming your using MS SQL please check that on table your are inserting and any related ones that the

1) Identity Specification = YES

2) Is Identity = YES

3) Identity Increment = 1

4) Identity Seed = 1

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

0

1)You may want to check about a circular dependency. It may be between the two entities Save Product with detail entities or viceversa

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

2)Be careful when you add Repository.Properties.AddObject(P); because in the code behind the P entity still has a reference to Detail table.

Make sure the detail related entity doesn't already save the objects that are linked to it.

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

0

Guid's are good for allowing you to create unique id without a database.

if your using int you will need to ensure that you have in the edmx model to be "Store Generated" for your primary key column.

0

Just replace this code in your 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 file add:

using System.Linq;

for used in multi threading:

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 better first you creat new instance of 'property' object then assign field by field varible then call AddObject and SaveChanged!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.