EF deleting an entity with some related entities RRS feed

  • 问题

  • These're my entities :class User{int UserId;  List<UserDetail> lstUserDetail;}

    class UserDetail {int UserDetailId; int Salary; int User_UserId ;}(Foreign Key)

    assume that I have a User(id=1) and two UserDetail(User_UserId = 1)

    I want to delete user : context.DeleteObject(user);

    The system throw an Exception : child record found

    when I use user.UserDetail.Load() or context.User.Include("UserDetail") It works correctly.

    I wonder Do I  have to manually load User‘s related entity?

    2013年7月1日 2:37


  • If there are some foreign keys in the table, and you cannot delete primary key record, because this will make your records uncomplete.

    Solution is you can create automatic cascading deleting by creating the relationship between the two tables by right clicking the "Diagram" folder and drag and drop the two tables, and then set CascadingDeleting=True.

    Thus when you delete the primary key, the foreign key will be also cascading deleting.

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    Found any spamming-senders? Please report at: Spam Report

    2013年7月1日 4:24