Rick P
Rick P

Reputation: 38

Some properties/enums are ignored when using code first migration

I'd like to add some properties to a entity to a model, but that property doesn't make any changes in the database nor in a migration script.

When I add this:

    public test test1 { get; set; }

    public enum test { asasas, asdasdasd };

I get an empty migration

    public override void Up()
    {
    }

    public override void Down()
    {
    }

But when i add:

    public String test1 { get; set; }

    public enum test { asasas, asdasdasd };

I get the expected migration

    public override void Up()
    {
        AddColumn("dbo.SpiderBatches", "test1", c => c.String());
    }

    public override void Down()
    {
        DropColumn("dbo.SpiderBatches", "test1");
    }

Because the second change does make a correct migration I can assume that the class/context are correctly setup and are working. So the error must be in the enum.

Can anyone help me further?

Upvotes: 1

Views: 660

Answers (2)

Adam Diament
Adam Diament

Reputation: 4840

Good answers above for .net 4.5, but ff you need to support .net 4.0 like me, then you can include the enum property you want to represent on your model as an int.

Two things -

1, hard code your values as per below to avoid the scenario of a change in their order in the .cs file destroying your referential integrity

public enum MyEnum
{
    FirstProperty = 1,
    AskUser = 2,
    IgnoreLine = 3,
    ImportPrice = 4,
    Undecided = 5
}

2 - When you save back to the database you will need to cast the enum as an int before saving it e.g.

MyEntity.PsuedoEnumProperty = (int)FirstProperty

Upvotes: 0

GotNoRealName2
GotNoRealName2

Reputation: 71

Enums are only supported when you target .NET 4.5, it is not supported for 4.0

Upvotes: 1

Related Questions