3

I have following code.

public class Person
{
    public string LastName { get; set; }
}

public class Employee : Person
{        
}

With configuration

Map(p => p.MapInheritedProperties());
Property(p => p.LastName).HasMaxLength(100).IsRequired();

And want to change it to

public class Person
{
        public virtual string LastName {get; set;}
}

public class Employee : Person
{
    public override string LastName
    {
       get { return base.LastName; }
       set 
       {
             //add validation here or throw exception
             base.LastName = value;
       }
    }
}

If I run the application it says that the model has been changed. Fine, I add a DB Migration, but it errors:

The property 'LastName' is not a declared property on type 'Employee'.

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.

What kind of mapping do I need to add for this to work? I use EF 4.3 with Migrations.

Thanks for any hints.

Community
  • 1
  • 1
Arne
  • 145
  • 1
  • 9

2 Answers2

1

This seems to be a limitation of EF- properties can either be on the base class or the subclasses, but not both. See How to share common column names in a Table per Hierarchy (TPH) mapping

Community
  • 1
  • 1
martin
  • 1,056
  • 8
  • 6
1

You can workaround that:

public class Person
{
    protected virtual void ValidateLastName() { }

    public string LastName
    {
       get { return lastName; }
       set 
       {
             ValidateLastName();
             lastName = value;
       }
    }
}

public class Employee : Person
{
    protected override void ValidateLastName()
    {
        // your validation logic here
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433