开发者

EF Codefirst, One class, multiple tables with discriminator

I doing a little investigation and I am wondering if the following is possible.

I am looking to create a BaseEntityWithDetails class that I can reuse 开发者_如何学运维for any type that I would like to have extendable. For example

public abstract class EntityDetail
{
}

This class is used to persist a key and value for the entity.

"Products" would be extended by doing the following...

public class ProductDetail : EntityDetail
{

}

public class Product : BaseEntityWithDetails<ProductDetail>
{

}

The base class "BaseEntityWithDetails" will provide some helper methods for setting and getting. What do you think?

What is the most effective way of mapping this with EF CodeFirst while being super easy to allow another type implement an DetailsEntityTypeConfiguration like the following

public class ProductMap : DetailsEntityTypeConfiguration<Product, ProductDetail>
{
}

Thanks in advance!


I would like to quote someone really smart on this: Reuse is a fallacy. Don't bother doing stuff like this because it will only make your design more obfuscated and complex. Save your inheritance to the entities in your domain which really share the same behavior, don't do this type of assumptions up front.

As a side note: You can map this as a table per type if you put your "EntityDetail" into your database, but as I said before, this is just not a good idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜