Jayson Knight
Jayson Knight

Reputation: 55

Entity Framework 6 Code First Error: The property 'Foo' is not a declared property on type 'Bar'

I'm getting the above error when attempting a db migration in EF 6.0. I am pretty sure this is due to the property being declared as abstract in a base class, then being overridden in a derived class. Here's some psuedocode to demonstrate this:

public abstract class Base
{
    [Required]
    public abstract double Amount { get; set; }
}

public abstract class Foo : Base
{
    // just showing that this class is in the inheritance chain, nothing is done with the Amount field
}

public class Bar : Foo
{
    [Required]
    public override double Amount { get; set; }
}

The exact error is:

The property 'Amount' is not a declared property on type 'Bar'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.

I have seen other SO threads that state to change the abstract property to virtual, but this is not an option as all derived classes MUST include the Amount property. If I need to add some mappings/configurations that is an option as well.

Upvotes: 1

Views: 864

Answers (1)

Ergwun
Ergwun

Reputation: 12978

I have seen other SO threads that state to change the abstract property to virtual, but this is not an option as all derived classes MUST include the Amount property. If I need to add some mappings/configurations that is an option as well.

If you make it virtual in the Base class, then all derived properties will include the Amount property. In fact as a simple value type property, it doesn't even need to be virtual.

I see no reason to force derived classes to override the property. If you other specific reasons for wanting the property to be overridden please edit them into your question.

Upvotes: 1

Related Questions