locked
Entity Framework Error: Multiple added entities may have the same primary key. RRS feed

  • Question

  • User1387190246 posted

    Hello. I am creating history of a record by clone that object. But when I called SaveChanges I got the error message Unable to determine the principal end of the 'ISTS.Core.Context.FK_SYS_GROUP_USER_USER_ID' relationship. Multiple added entities may have the same primary key, here is my code

    var auditRecord = new SYS_USER()
                {
                    SYS_TRAN_ID = 1195358,
                    AUDIT_FIELDS = null,
                    AUDIT_OPERATION = "I",
                    APPROVAL_STATUS = "I",
                    UPD_SEQ = 0,
                    AMND_STATE = "A",
                    USER_ID = "phongbv",
                    USER_NAME = "Bui Van Phong",
                    DEPT_ID = 883,
                    PASS_CODE = "CEEA23519F6F86AD67E9F798BF8002CB",
                    STATUS = "A",
                    EFFECTIVE = DateTime.Now,
                    LAST_LOGIN = null,
                    EXPIRY = null,
                    TRY_COUNT = null,
                    MAX_CONNECTIONS = null,
                    USER_LEVEL = "9",
                    LDAP_ID = null,
                    AUTO_AUTHORIZED = 0,
                    SID = 61726161,
                    INHERIT_MENU_ACL = 1,
                    USER_GROUPS = new List<SYS_GROUP_USER>()
                    {
                        new SYS_GROUP_USER()
                        {
                            GROUP_ID = 224,
                            SID = 61726162,
                            AMND_STATE = "A",
                        }
                    }
                };
                var finalRecord = new SYS_USER()
                {
                    SYS_TRAN_ID = 1195358,
                    AUDIT_FIELDS = null,
                    AUDIT_OPERATION = "I",
                    APPROVAL_STATUS = "I",
                    UPD_SEQ = 0,
                    AMND_STATE = "F",
                    USER_ID = "phongbv",
                    USER_NAME = "Bui Van Phong",
                    DEPT_ID = 883,
                    PASS_CODE = "CEEA23519F6F86AD67E9F798BF8002CB",
                    STATUS = "A",
                    EFFECTIVE = DateTime.Now,
                    LAST_LOGIN = null,
                    EXPIRY = null,
                    TRY_COUNT = null,
                    MAX_CONNECTIONS = null,
                    USER_LEVEL = "9",
                    LDAP_ID = null,
                    AUTO_AUTHORIZED = 0,
                    SID = 61726161,
                    INHERIT_MENU_ACL = 1,
                    USER_GROUPS = new List<SYS_GROUP_USER>()
                    {
                        new SYS_GROUP_USER()
                        {
                            GROUP_ID = 224,
                            SID = 61726162,
                            AMND_STATE = "F",
                        }
                    }
                };
                AppContext.TransactionContext.SYS_USER.Add(auditRecord);
                AppContext.TransactionContext.SYS_USER.Add(finalRecord);
                AppContext.TransactionContext.SaveChanges();

    Saturday, August 19, 2017 1:44 PM

Answers

  • User-1716951449 posted

    not about ur issue, but

    is ur code sample showing something "sensitive" data? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 21, 2017 2:28 AM

All replies

  • User-1716951449 posted

    not about ur issue, but

    is ur code sample showing something "sensitive" data? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 21, 2017 2:28 AM
  • User1387190246 posted

    I tried with other project it worked but with current project it is not working. 

    Monday, August 21, 2017 3:37 AM
  • User-1716951449 posted

    i have not given u any answer to ur question, dunno why u mark my feedback as answer

    next time do urself a favor, do some "common sense" over codes u gonna post on internet

    Monday, August 21, 2017 4:50 AM
  • User-271186128 posted

    Hi phongbv,

    Unable to determine the principal end of the 'ISTS.Core.Context.FK_SYS_GROUP_USER_USER_ID' relationship. Multiple added entities may have the same primary key,

    According to your code, the two records contain the same SYS_TRAN_ID, DEPT_ID, SID and GROUP_ID. But the primary key couldn’t be repeated.

    So I suggest you could change the value of primary key.

    Besides, you could also try to use the identity attribute and auto-generate the primary key.

    And, from your code, when you add relevant entities, I suggest you could check whether the entities is exist or not, if the entities is exist, you could use the existed model, instead of creating a new model.

    Best regards,
    Dillion

    Monday, August 21, 2017 9:57 AM