locked
Unable to remove entity RRS feed

  • Question

  •             ServiceType serviceType = linkwebDB.ServiceTypes.FirstOrDefault();
                ServiceTypeCharge chargeEntryToRemove = serviceType.ServiceTypeCharges.OrderByDescending(currentServiceType => currentServiceType.IndexLocation).ElementAt(2);
    
                serviceType.ServiceTypeCharges.Remove(
                    chargeEntryToRemove);
    
                linkwebDB.SaveChanges();

    I have the code above, but when the linkwebDB.SaveChanges() execute i get a dbupdateexception exception. 

    Why am i getting a cannot insert the value null into column when i'm trying to remove?


    Knowledge is Power, Guard it Well!!


    Friday, June 6, 2014 12:47 AM

Answers

  • Hello,

    >>Why am i getting a cannot insert the value null into column when i'm trying to remove?

    This is because this line below is not to delete the record from the database, it is used to delete the relationship between your two tables. It will try to set the foreign key filed to null however, this field should be non-null filed.

    serviceType.ServiceTypeCharges.Remove(chargeEntryToRemove);

    If you want to delete this record, writing codes as:

    linkwebDB. ServiceTypeCharges.Remove(chargeEntryToRemove);

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 6, 2014 9:30 AM

All replies

  • Hello,

    >>Why am i getting a cannot insert the value null into column when i'm trying to remove?

    This is because this line below is not to delete the record from the database, it is used to delete the relationship between your two tables. It will try to set the foreign key filed to null however, this field should be non-null filed.

    serviceType.ServiceTypeCharges.Remove(chargeEntryToRemove);

    If you want to delete this record, writing codes as:

    linkwebDB. ServiceTypeCharges.Remove(chargeEntryToRemove);

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 6, 2014 9:30 AM
  • Hi Fred,

    That worked!!!

    Thanks buddy.


    Knowledge is Power, Guard it Well!!

    Friday, June 6, 2014 11:18 AM