2013-05-31 124 views
1

有一個使用EF Code First開發的MVC3應用程序。該解決方案內有三個項目; ProjectName.Model,ProjectName.MVC,Project.Repository。在模型項目中的POCO類中,有一個「人」類,「用戶」從該類繼承。至於涉及到加載「Create.cshml」,下面的錯誤顯示:如何首先解決EF代碼中的映射問題?

錯誤3032:問題在映射片段起始於線17,29:EntityTypes ProjectName_Repository.Man,ProjectName_Repository.User被映射到表中的同一行Man。映射條件可用於區分這些類型映射到的行。

我已周圍像鏈接一看:http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspxhttp://forums.asp.net/t/1735718.aspx/1

,但無法找出有用的東西。

這裏我的模型類:

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; } 
} 

和派生類:

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._%+-][email protected][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; } 
} 

所以,任何人都可以幫助我在此懇求? 欣賞..

回答

2

在繼承中,如果您從absrtact基類繼承並行類,它會更好。我甚至認爲EF不能支持具體類型的繼承層次結構。

我想創建一個基類像ManBase並從中

public abstract class ManBase 
public class Man : ManBase 
public class User: ManBase 

派生兩類您可以選擇最適合的繼承策略。它可能是TPH(如你所提到的鏈接)。

還有一個建築效益:現在您可以更改Man類,而不會對User產生任何影響。在你現在的模型中是不可能的。

+0

我感謝您的回覆,並且您關心我的問題。我要測試你的建議。 – user2394196