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.

4

2 回答 2

1

您可以解决此问题:

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
    }
}
于 2012-07-03T07:36:42.010 回答
1

这似乎是 EF 的一个限制,属性可以在基类或子类上,但不能同时在两者上。请参阅如何在按层次结构 (TPH) 映射的表中共享通用列名

于 2012-07-03T07:32:43.353 回答