locked
Content_Title' AssociationSet is in the 'Added' state. Given multiplicity constraints, a corresponding 'Content_Title_Source' must also in the 'Added' state. RRS feed

  • Question

  • User2101084149 posted

    Hello i need a litle help with my project .in this url

    when i try the to edit content by going to editcontent  command in the contentcontroller i get the

    System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details. ---> System.Data.Entity.Core.UpdateException: A relationship from the 'Content_Title' AssociationSet is in the 'Added' state. Given multiplicity constraints, a corresponding 'Content_Title_Source' must also in the 'Added' state.

    Exception any idea what is causing it ?

    the models are

    Title

    Content

    any idea  hot to fix it ?

    I dont want to change all the models only the Content and title one

    Tuesday, October 13, 2015 5:55 PM

Answers

  • User-271186128 posted

    Hi angarato_surion,

    According to your code, it seems that you want to configure a One-to-One relationship between Content and Title, so, in the Title class, you also need to add the content class reference. You could refer to the following code:

        public class Student
        {
            public Student() { }
    
            public int StudentId { get; set; }
            public string StudentName { get; set; }
    
            public virtual StudentAddress StudentAddress { get; set; }
    
        }
         
        public class StudentAddress 
        {
            [Key, ForeignKey("Student")]
            public int StudentId { get; set; }
            
            public string Address1 { get; set; }
            public string Address2 { get; set; }
            public string City { get; set; }
            public int Zipcode { get; set; }
            public string State { get; set; }
            public string Country { get; set; }
    
            public virtual Student Student { get; set; }
        }
         

    For more details, please see: http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx

    System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while saving entities that do not expose foreign 
    key properties for their relationships. The EntityEntries property will return null because a single entity cannot be
    identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign
    key properties in your entity types. See the InnerException for details. ---> System.Data.Entity.Core.UpdateException:
    A relationship from the 'Content_Title' AssociationSet is in the 'Added' state. Given multiplicity constraints, a
    corresponding 'Content_Title_Source' must also in the 'Added' state.

    As for this issue, I suggest you could refer to the following threads:

    http://stackoverflow.com/questions/8115480/adding-detached-entities-to-a-1-many-relationship-in-ef-4-1-code-first

    http://stackoverflow.com/questions/17850702/a-relationship-is-in-the-deleted-state

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 13, 2015 10:25 PM