1

I get this error Cannot add an entity with a key that is already in use. when I run the code below.

Tables: enter image description here

What am i missing?

    private void CopyAllPageObjects(int fromPageID, int toPageID)
    {
        CMSDataContext _db = new CMSDataContext();

        // Copy page objects
        var originalPageObjects = (from x in _db.CMSPageObjects
                                   where x.PageID == fromPageID
                                   select x);

        List<CMSPageObject> newPageObjects = new List<CMSPageObject>();
        foreach (CMSPageObject po in originalPageObjects)
        {
            CMSPageObject newPageObject = new CMSPageObject();
            newPageObject.PageID = toPageID;
            newPageObject.CMSObjectID = po.CMSObjectID;
            newPageObject.Name = po.Name;
            newPageObject.Sorting = po.Sorting;
            newPageObjects.Add(newPageObject);

            // Copy page object attribute values
            var originalPoavs = (from x in _db.CMSPageObjectAttributeValues
                                 where x.CMSPageObjectID == po.ID
                                 select x);

            List<CMSPageObjectAttributeValue> newPoavs = new List<CMSPageObjectAttributeValue>();
            foreach (CMSPageObjectAttributeValue poav in originalPoavs)
            {
                CMSPageObjectAttributeValue newPoav = new CMSPageObjectAttributeValue();
                newPoav.CMSAttributeID = poav.CMSAttributeID;
                newPoav.CMSPageObjectID = newPageObject.ID;
                newPoav.LCID = poav.LCID;
                newPoav.Value = poav.Value;
                newPoavs.Add(newPoav);
            }
            _db.CMSPageObjectAttributeValues.InsertAllOnSubmit(newPoavs);
        }

        _db.CMSPageObjects.InsertAllOnSubmit(newPageObjects);
        _db.SubmitChanges();
    }
Martin at Mennt
  • 5,677
  • 13
  • 61
  • 89

4 Answers4

4

I was getting this error and it was because I had forgotten to set the Primary Key field in the database to "Identity Specification" (auto-increment). But that is just a guess

Arion
  • 31,011
  • 10
  • 70
  • 88
3

It looks like you're trying to add an object, while another one with same primary key exists. Are PageID or CMSObjectID primary keys? Or CMSAttributeID?

You might also want to share more data about how your data tables look like.

Update: after you added database struct, I would look closer at this line:

newPoav.CMSPageObjectID = newPageObject.ID;

the newPageObject.ID is probably not known at this time, because you didn't add the object to the DB yet (I suspect ID is identity). I think you could use:

newPoav.CMSPageObject = newPageObject
Arek
  • 1,276
  • 1
  • 10
  • 19
2

Seems you are missing primary key or an unique key on CMSPageObject table. Please try to verify the keys in the database. I had same issue since I had missed the PK on the table.

Cheers.

Shailesh
  • 1,178
  • 11
  • 12
0

you have to add some code just for testing if the list newPoavs have a key exist already in the database

you can just add this

        foreach (CMSPageObjectAttributeValue poav in originalPoavs)
        {
            CMSPageObjectAttributeValue newPoav = new CMSPageObjectAttributeValue();
            newPoav.CMSAttributeID = poav.CMSAttributeID;
            newPoav.CMSPageObjectID = newPageObject.ID;
            newPoav.LCID = poav.LCID;
            newPoav.Value = poav.Value;
            newPoavs.Add(newPoav);
            if(_db.CMSPageObjectAttributeValues.Any(x=>x.LCID == newPoav.LCID & x.CMSAttributeID == newPoav.CMSAttributeID & x.CMSPageObjectID == newPoav.CMSPageObjectID ))
            MessageBox.Show("Already exist");
        }

just to test your values

Akrem
  • 5,033
  • 8
  • 37
  • 64