2
votes

I have problems mapping a overrided property from a POCO object to database using TPH in Code First.

My code is similar to these classes:

public abstract class Vehicle
{
    public int ID { get; set; }
    public abstract NumberOfWheels { get; set;}
}

public class Motorbike: Vehicle
{
    public override NumberOfWheels { get; set; }
}

When I try to save the Motorbike class to Database I get the next message

The property 'NumberOfWheels' is not a declared property on type 'Motorbike'. 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.

It seems like code first can't map override properties. Is there any suggestion how to map an override property with Code first?

Thanks

2

2 Answers

2
votes

Does the Property, in the Base Class have a [NotMapped] attribute? That can cause the derived class not to save it's property value to the corresponding database table. In that case, you will need another property that the overridden property can forward the value to.

public partial class BaseClass
{
    [NotMapped]
    public virtual int SequenceNumber { get; set; }

}


[Table("TABLE_TEST", Schema = "dbo")]
public partial class DerivedClass : BaseClass
{

    public override int SequenceNumber
    {
        get
        {
            return this.SeqNumField;
        }
        set
        {
            this.SeqNumField = value;
        }
    }

    // This is the property pointing to the real database column
    [Column(TypeName = "int"), Required]
    public int SeqNumField { get; set; }


}
-1
votes

The problem is not in overriding the property but in defining property as abstract in a base class. Use virtual property in the base class instead and it should work.