locked
How to fix “EntityState.Modified” problem in asp.net MVC5 RRS feed

  • Question

  • User1300771107 posted

    when I update data , I get EntityState.Modified crash . and I get this message "An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

    Additional information: A referential integrity constraint violation occurred: The property value(s) of 'Person.PersonId' on one end of a relationship do not match the property value(s) of 'Contacts.PersonId' on the other end."

    Thanks in advance .

    Friday, April 12, 2019 12:56 PM

All replies

  • User475983607 posted

    The error means there is a bug in your code where the value of Contacts.PersonId does not exist in the Person table.  I assume the PresonId is zero but we cannot see your code.

    Friday, April 12, 2019 1:03 PM
  • User1300771107 posted

    thanks for your reply . this is just an exemple I have another tables <Folder and Trainee> I have TraineeId in the two tables , as a primary key in table Trainee and as a foreign key in table Folder

    this is trainee code :

    namespace internship.Models
    {
        using System;
        using System.Collections.Generic;
        
        public partial class Trainee
        {
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
            public Trainee()
            {
                this.Folder = new HashSet<Folder>();
            }
        
            public int traineeID { get; set; }
            public string cin { get; set; }
            public string fullName { get; set; }
            public string institution { get; set; }
        
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<Folder> Folder { get; set; }
        }
    }
    

    and this is Folder code :

    namespace internship.Models
    {
        using System;
        using System.Collections.Generic;
        using System.Web.Mvc;
    
    
        public partial class Folder
        {
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
            public Folder()
            {
                this.Period = new HashSet<Period>();
            }
        
            public int folderID { get; set; }
            public string officeOrderNumber { get; set; }
            public Nullable<System.DateTime> officeOrderDate { get; set; }
            public int? traineeID { get; set; }
            public Nullable<System.DateTime> registrationDate { get; set; }
            public string status { get; set; }
            public Nullable<int> folderNumber { get; set; }
            public Nullable<int> facilityID { get; set; }
            public Nullable<int> serviceID { get; set; }
            public Nullable<int> cityID { get; set; }
    
            
            public virtual City City { get; set; }
            public virtual Facilities Facilities { get; set; }
            public virtual Services Services { get; set; }
            public virtual Trainee Trainee { get; set; }
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<Period> Period { get; set; }
    
            public IEnumerable<SelectListItem> citiesList { get; set; }
            public IEnumerable<SelectListItem> facilitiesList { get; set; }
            public IEnumerable<SelectListItem> servicesList { get; set; }
        }
    }
    

    Friday, April 12, 2019 3:06 PM
  • User1724605321 posted

    Hi Learner0101,

    Your second code samples have the same error with your first reply ? That seems the Folder and Trainee are one-to-many relationship , Can you show the codes where cause the error ?

    Best Regards,

    Nan Yu

    Monday, April 15, 2019 2:13 AM