我如何首先解决EF代码中的映射问题?

时间:2013-05-31 09:58:58

标签: c# asp.net-mvc-3 ef-code-first

使用EF Code First开发了一个MVC3应用程序。该解决方案内部有三个项目; ProjectName.Model,ProjectName.MVC,Project.Repository。在模型项目内的POCO类中,有一个'Man'类,从中继承了'User'。在加载'Create.cshml'时,会显示以下错误:

  

错误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.aspx这样的链接 http://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._%+-]+@[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; }
}

那么,有人可以帮我解决这个问题吗?! 感谢..

1 个答案:

答案 0 :(得分:2)

在继承中,如果你有一个并排继承自absrtact基类的类,它会更好。我甚至认为EF不支持具体类型的继承层次结构。

我会创建一个像ManBase这样的基类,并从中派生出两个类

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

您可以选择最适合的继承策略。它可能是TPH(如你提到的链接)。

还有一个架构优势:现在您可以更改Man类,而不会对User产生任何影响。在你当前的模型中是不可能的。