简体   繁体   English

派生类中的EF6枚举错误

[英]EF6 enum error in derived class

I have a base class Company and two derived classes Retailer and Supplier . 我有一个基类Company和两个派生类RetailerSupplier 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: 我必须在Retailer类中添加一个枚举,但是现在当我尝试创建新的Supplier ,出现以下SQL错误:

Cannot insert the value NULL into column 'OperatingStatus', table 'Company'; 无法将值NULL插入表'Company'的'OperatingStatus'列中; column does not allow nulls. 列不允许为空。 INSERT fails. INSERT失败。 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 . 我需要做的就是将其默认设置为0

Traditionally, there are 2 ways to implement "Inheritance" in Database.. and EF does support both of them. 传统上,有两种方法可以在数据库中实现“继承”。并且EF确实支持这两种方法。 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. 在这种情况下,您需要使该字段为Nullable(可以为null),以便数据不会出现错误。 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 http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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