New inherited class gets its property name renamed on migration with EF Code First

c# entity-framework entity-framework-6 inheritance

Question

I'm using Entity Framework Code First with inheritance to define my classes each implementing an abstract class containing common properties all of them should have.

I ran into a problem when I wanted to add new one to batch. All inherited classes have property having a common name with different type, but in this last class, property had same type as well.

public abstract class ProductProperty
{
    public string CommonProp { get; set; }
}


public class IntegerProperty : ProductProperty
{
    [Required]
    public int Value { get; set; }
}

public class StringProperty : ProductProperty
{
    [Required]
    public string Value { get; set; }
}

public class DoubleProperty : ProductProperty
{
    [Required]
    public double Value { get; set; }
}

Up to this point things where good. When I've added another one with integer Value property, Code First Migration is created by moving existing Value column on SQL table, instead of creating new one, which of course causes problem, because I already have data in Properties table.

public class IntWithUnitProperty : ProductProperty
{
    [Required]
    public int Value { get; set; }

    public string Unit { get; set; }
}

RenameColumn(table: "dbo.ProductProperties", name: "Value1", newName: "Value2");
RenameColumn(table: "dbo.ProductProperties", name: "Value2", newName: "Value3");
AlterColumn("dbo.ProductProperties", "Value1", c => c.Int());
AlterColumn("dbo.ProductProperties", "Value2", c => c.String());

It is obvious this is wrong in multiple ways yet I've tried to update database:

Error: The new name 'Value2' is already in use as a COLUMN name and would cause a duplicate that is not permitted.

From this behaviour, what I understand is EF starts giving names and suffixes by class name order:

  1. Value for Double
  2. Value1 for Int
  3. Value2 for String

And when I've tried to add another class with name starting Int... into batch, it tries to put it after Value1.

Question is, where are column names with number suffixes pointing properties with same name of inherited classes mapped in EF?

Maybe I can tell EF to add a new column in SQL and point to right class instead of trying to manipulate existing ones.

1
2
12/11/2015 8:15:28 PM

Accepted Answer

I think you'd be better off explicitly naming your Value columns by using data annotations, rather than risking them being re-assigned during migration. For example:

public class IntegerProperty : ProductProperty
{
    [Required]
    [Column("Value1")]
    public int Value { get; set; }
}

public class StringProperty : ProductProperty
{
    [Required]
    [Column("Value2")]
    public string Value { get; set; }
}

And so on.

Alternatively you could use table-per-type (TPT) rather than the default table-per-hierarchy (TPH), so that each subclass has it's own table, and therefore Value will always be called Value. Example: http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt

2
12/8/2015 11:57:57 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow