Reputation: 341
There is an MVC3 application being developed using EF Code First. The solution has three projects inside; ProjectName.Model, ProjectName.MVC, Project.Repository. In POCO classes inside the model project there is a 'Man' class from which the 'User' is inherited. As it comes to loading the 'Create.cshml', the following error display:
error 3032: Problem in mapping fragments starting at lines 17, 29:EntityTypes ProjectName_Repository.Man, ProjectName_Repository.User are being mapped to the same rows in table Man. Mapping conditions can be used to distinguish the rows that these types are mapped to.
I have taken a look around links like: http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx and also http://forums.asp.net/t/1735718.aspx/1
But couldn't find out something helpful.
Here my model classes:
public class Man
{
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public long ID { get; set; }
//------------------------------------------------------------//
[Required, MaxLength(20)]
[LocalizedAttribute("FName")]
public string FName { get; set; }
//------------------------------------------------------------//
[Required, MaxLength(20)]
[LocalizedAttribute("LastName")]
public string LastName { get; set; }
//------------------------------------------------------------//
[Required]
[RegularExpression("^[0-9]+$", ErrorMessageResourceName = "ErrorNumberOnly", ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources))]
[LocalizedAttribute("Mobile")]
public string Mobile { get; set; }
//------------------------------------------------------------//
[LocalizedAttribute("Phone")]
[RegularExpression("^[0-9]+$", ErrorMessageResourceName = "ErrorNumberOnly", ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources))]
public string HomePhone { get; set; }
//------------------------------------------------------------//
[RegularExpression("^[0-9]+$")]
[LocalizedAttribute("IDCardNumber")]
public string IDCardNumber { get; set; }
//------------------------------------------------------------//
[RegularExpression("^[0-9]+$")]
[LocalizedAttribute("NationalCode")]
public string NationalCode { get; set; }
//------------------------------------------------------------//
[MaxLength(10)]
[LocalizedAttribute("DOB")]
public int DOB { get; set; }
//------------------------------------------------------------//
[Required]
public int CityID { get; set; }
[ForeignKey("CityID")]
[LocalizedAttribute("City")]
public virtual City CityParent { get; set; }
//------------------------------------------------------------//
[MaxLength(100)]
[LocalizedAttribute("Address")]
public string Address { get; set; }
//------------------------------------------------------------//
[LocalizedAttribute("PostalCode")]
public string PostalCode { get; set; }
//------------------------------------------------------------//
[MaxLength(255)]
[LocalizedAttribute("PhotoPath")]
public string PhotoPath { get; set; }
}
and the derived class:
public class User : Man
{
[MaxLength(20)]
[LocalizedAttribute("Username")]
public string UserName { get; set; }
//------------------------------------------------------------//
[DataType(DataType.Password)]
[MaxLength(100), MinLength(6, ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources), ErrorMessageResourceName = "ErrorPasswordLength")]
[LocalizedAttribute("Password")]
public string Password { get; set; }
//------------------------------------------------------------//
[DataType(DataType.Password)]
[Compare("Password", ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources), ErrorMessageResourceName = "ErrorConfirmPassword")]
[LocalizedAttribute("ConfirmPassword")]
public string ConfirmPassword { get; set; }
//------------------------------------------------------------//
[DataType(DataType.EmailAddress, ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources), ErrorMessageResourceName = "ErrorEmailInvalid")]
[MaxLength(20)]
[RegularExpression(@"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")]
[LocalizedAttribute("Email")]
public string Email { get; set; }
//------------------------------------------------------------//
[MaxLength(30)]
[LocalizedAttribute("Title")]
public string Title { get; set; }
//------------------------------------------------------------//
[MaxLength(10)]
[LocalizedAttribute("HireDate")]
public int HireDate { get; set; }
//------------------------------------------------------------//
[LocalizedAttribute("ReportsTo")]
public long ReportsTo { get; set; }
[ForeignKey("ReportsTo")]
public virtual IList<User> ReportsChild { get; set; }
}
So, anyone can help me on this pleas?! Appreciate that..
Upvotes: 1
Views: 760
Reputation: 109117
In inheritance it works much better if you have classes side-by-side inheriting from an absrtact base class. I even think that EF can't support an inheritance hierarchy of concrete types.
I would create a base class like ManBase
and derive your two classes from it
public abstract class ManBase
public class Man : ManBase
public class User: ManBase
You can choose an inheritance strategy that fits best. It could be TPH (as in the link you mention).
There is also a architectural benefit: now you can change the Man
class without any implications for User
. In your current model that is impossible.
Upvotes: 2