当前位置: 动力学知识库 > 问答 > 编程问答 >

Deleting child entities in Entity Framework w/ Repository Pattern

问题描述:

I have a child entity:

public class PhoneNumber : KeyedEntityBase

{

public string Number { get; set; }

public string Extension { get; set; }

}

that exists in multiple parents:

public class Customer : KeyedEntityBase

{

public string Name { get; set; }

public PhoneNumber PhoneNumber { get; set; }

}

public class Vendor : KeyedEntityBase

{

public string VendorName { get; set; }

public PhoneNumber PhoneNumber { get; set; }

}

When I want to delete a PhoneNumber from one of the parents I just null it out. Is there a way I can make Entity Framework handle deleting orphan PhoneNumber instances when using a Repository pattern? I believe NHibernate's all-delete-orphan cascade mode does this. Now to do this I have to create a PhoneNumberRepository and explicitly delete the PhoneNumber after I null it out on the parent, and this smells.

分享给朋友:
您可能感兴趣的文章:
随机阅读: