简体   繁体   中英

EF6 enum error in derived class

I have a base class Company and two derived classes Retailer and Supplier . I had to add an enum to the Retailer class but now when I try to create a new Supplier I get the following SQL error:

Cannot insert the value NULL into column 'OperatingStatus', table 'Company'; column does not allow nulls. INSERT fails. The statement has been terminated.

In case it helps, here are the classes and the enum:

public abstract class Company
{
    ...
}

public class Retailer : Company
{
    public OperatingStatus OperatingStatus { get; set; }
    ...
}

public class Supplier : Company
{
    ...
}

public enum OperatingStatus
{
    Unknown = 0,
    Active = 1,
    Inactive = -1
}

Is there a way to make this work without moving the enum to the base class? All I need is to make it defaults to 0 .

Traditionally, there are 2 ways to implement "Inheritance" in Database.. and EF does support both of them. You seem that you have chosen to have all the classes in one single big Table (Table per hierarchy). In that case, you need to make that field Nullable (Can be null) so that data will not complain. You still can validate that field on the Business Object level not the database level.

The other way (Table per concrete class) is to make separate tables one only for the new properties in Suppliers and and another for district properties in retailers.. That would be the second solution for inheritance.

http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM