none
Edit/Delete child from the context issue RRS feed

  • Question

  • Hi, All

    I am developing one site using asp.net mvc 3 with EntityFramework 4.0 (also using common repository layer).

    I am facing error some times when I delete or update child object (or I can say that the object which has relationship with other business object like one to many or many to many).

    EF throws this error some times not every time, that means its not constantly throws the error but after 2 or 3 times update/delete operation I faces this error.

    I don't now what is the reason behind that.

    Error thrown from the Edmx is:

    The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. 

    Code Example

    List<DeviceGroupDetail> dgdetail = this.DeviceGroupDetailSelectAll().Where(x => x.DeviceID == device.DbId).ToList();
    foreach (DeviceGroupDetail dg in dgdetail)
    {
       this.context.DevicegroupDetails.Delete(dg);
       this.context.Savechanges();
    }

    If anyone knows please help me.

    Thanks

     

    Regards, Hiren Bharadwa


    Saturday, December 29, 2012 1:53 PM

All replies

  • Hi Hiren,

    The error message told all things, follow it.


    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Monday, December 31, 2012 6:15 AM
  • Hello, ghost

    I read many articles on this and almost solve this problem for delete/edit. but now I am facing the same problem when I insert new object/entity to the database using edmx.

    I have a class named ApplicationEvent and it has a foreign key of EventTypeList table which is nullable field.

        public class ApplicationEvent
        {
            
            public virtual Guid DbId
            {
                get;
                set;
            }
    
           
            public virtual Guid EvenTypeId
            {
                get;
                set;
            }
    
            
            public virtual EventTypeList EventTypeList
            {
                get;
                set;
            }
        }
    
     public class EventTypeList
        {
           
            public virtual Guid DbId
            {
                get;
                set;
            }
    
           
            public virtual string Type
            {
                get;
                set;
            }
            
            public virtual Guid? ModuleId
            {
                get;
                set;
            }
    
            public virtual Module Module
            {
                get;
                set;
            }
    
            public virtual List<ApplicationEvent> ApplicationEvents
            {
                get;
                set;
            }
        }

    When I insert new ApplicationEvent. It throws the same error.

    I don't know where I am doing mistake. and this error not comes constantly...(i.e after some number of applicationevent created)


    Regards, Hiren Bharadwa




    Monday, December 31, 2012 6:30 AM
  • Hi Hiren,

    You said you are using edmx, so I think you are using Database First or Model First. However, the class definition you provided seems you changed something in original generated classes. Is that right? I am not sure whether all the properties of ApplicatinEvent objects you tried to insert have the corresponding values or not. Maybe you have not assign the foreign key value and navigation property. If it is convenient, please provide some code snippet for inserting part.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 2, 2013 6:01 AM