Kuttan Sujith
Kuttan Sujith

Reputation: 7979

Inheritance with the Entity Framework Code first, field repeats in table

Class GeneralRequest 
{    
String BaseProperty { get set}
}

RapidRequest: GeneralRequest
{
Bool IsSignRequired { get set}    
}

BackRequest : GeneralRequest
{
Bool IsSignRequired { get set}    
}

OtherRequest: GeneralRequest
{   
String Note   { get set}   
}

This is the structure of codes I have in my project.

The property IsSignRequired comes in BackRequest and RapidRequest but it need not come in OtherRequest.

But when I generate database using this codes.

I get property IsSignRequired comes twice in my table as IsSignRequired1 IsSignRequired2 etc.

How can I specify that it is only neded once in my table

Upvotes: 1

Views: 91

Answers (1)

Bradley Uffner
Bradley Uffner

Reputation: 16991

You either need to have another intermediate class that RapidRequest and BackRequest inherits from that defines an inherited IsSignRequired, or use the ColumnAttribute to force the name of the field. Another option is to use TablePerType inheritance, but that will generate separate tables for unique fields for each subclass.

EntityFramework is doing this because the field is declared independently on the subclasses. By default each subclass will get unique copies of any fields defined in that class, only inherited fields will be shared.

Upvotes: 1

Related Questions