Using base class for object creates problems for EF4.1 POCO
On a new project I'd like to use the "Code First" feature of Entity Framework to set up my data store.
In my project I may have multiple types of People (a ticket holder, a traveller, a selling agent, etc) but I only want validation on the ticket holder. So I created a base class of type "Person" that does not have the validation attributes and an inherited class of type "TicketHolder" that contains the validation for First Name, etc).
My problem is that EF is throwing exceptions:
One or more validation errors were detected during model generation: System.Data.Edm.EdmProperty: Name: Each property name in a type must be unique. Property name 'Title' is already defined.
Looks like EF is not recognising that the properties of the "TicketHolder" override the properties of the "Person".
How do I get round this?
Base Class:
public abstract class Person
{
public int Id { get; set; }
public virtual string Title { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
}
Derived class for TicketHolder:
public class TicketHolder : Person
{
[Required(ErrorMessage = "Title Required")]
public override string Title { get; set; }
[Required(ErrorMessage = "First Name Required")]
public override string FirstName { get; set;开发者_JAVA百科 }
[Required(ErrorMessage = "Last Name Required")]
public override string LastName { get; set; }
}
Try abstract
instead of virtual
:
public abstract class Person {
public int Id { get; set; }
public abstract string Title { get; set; }
public abstract string FirstName { get; set; }
public abstract string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
}
You might also consider using MetadataTypeAttribute to separate metadata from entity.
精彩评论