0

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.

Henrique Miranda
  • 1,030
  • 1
  • 13
  • 31
  • If this is TPH as it seems, the `OperatingStatus` column in the `Company` table **should** allow `NULL`, how did you create it? – Ivan Stoev Jul 24 '17 at 18:48
  • ``Company`` does not have this property. – Henrique Miranda Jul 25 '17 at 21:13
  • According to the error message, it does - `Company` **table**, not class. You don't need to use nullable type in the class, EF migration would have created nullable **column** in the database **table**, that's why I was asking whether the table/column was created by EF or manually by you. – Ivan Stoev Jul 25 '17 at 22:43
  • I was trying to solve it using data anotations/fluent api but I had no sucess so I created a new migration to allow for nulls and problem solved. Thanks. – Henrique Miranda Aug 31 '17 at 18:58

1 Answers1

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

Assil
  • 572
  • 6
  • 21
  • Yes, I use TPH inheritance. What's the best way to make it nullable? Simply by setting it as nullable ``public OperatingStatus? OperatingStatus`` doesn't seem to do anything because when I try to add a migration it comes empty. – Henrique Miranda Jul 25 '17 at 21:18
  • Is worth noting that I would rather not make it nullable in the class because it causes all sorts of problems when making queries. A better solution would be it defaulting to ``0`` but I don't know how to make it so. – Henrique Miranda Jul 25 '17 at 21:21
  • Please have a look at: Entity Framework 6 Code first Default value: https://stackoverflow.com/questions/19554050/entity-framework-6-code-first-default-value – Assil Jul 25 '17 at 22:32