我正在开展一个小项目,我有多层次的一对多模型。但由于某种原因,EF似乎不理解这种关系,当我用EF创建MVC控制器时收到错误。
以下是我的模型类:
public class BusinessDomain
{
[Key]
public int BusinessDomainID { get; set; }
[Display(Name = "Business Domain Name")]
public string DomainName { get; set; }
[Display(Name = "Domain Manager Name")]
public string DomainManager { get; set; }
public virtual ICollection<BusinessProcess> BusinessProcesses { get; set; }
}
public class BusinessProcess
{
public int BusinessProcessID { get; set; }
[Display(Name = "Business Processs Name")]
public string ProcessName { get; set; }
[Display(Name = "Business Process Owner Name")]
public string ProcessOwner { get; set; }
public virtual ICollection<BusinessSubProcess> BusinessSubProcesses { get; set; }
public int BusinessDomainID { get; set; }
public virtual BusinessDomain BusinessDomain { get; set; }
}
public class BusinessSubProcess
{
public int BusinessSubProcessId { get; set; }
public string SubProcessName { get; set; }
public string SubProcessOwnerName { get; set; }
public int BusinessProcessID { get; set; }
public virtual BusinessProcess BusinessProcess { get; set; }
}
当我创建EF控制器时,我得到以下错误:依赖角色是指关键属性,Dependent角色的多重性的上限必须为1.
如果我只保留两个模型(BusinessLine和BusinessProcess),它似乎可行。我开始怀疑ASP MVC是否不支持一对多模型的多级?
请告诉我,我做错了什么?
提前致谢!
答案 0 :(得分:0)
我看到的唯一区别是您没有在BusinessProcess类中包含对BusinessLine对象的引用。您应该包括它并将其标记为外键。
此外,您将BusinessSubprocessID标记为BusinessProcess的外键,我认为这是一个错误,因为您还在类中持有BusinessProcessID。
作为旁注,您是否需要Subprocess类中的BusinessLineID,因为您可以通过其父级轻松访问它?
public class BusinessLine
{
[Key]
public int BusinessLineID { get; set; }
[Display(Name = "Business Domain Name")]
public string DomainName { get; set; }
[Display(Name = "Domain Manager Name")]
public string DomainManager { get; set; }
public virtual ICollection<BusinessProcess> BusinessProcesses { get; set; }
}
public class BusinessProcess
{
[Key]
public int BusinessProcessID { get; set; }
[Display(Name = "Business Processs Name")]
public string ProcessName { get; set; }
[Display(Name = "Business Process Owner Name")]
public string ProcessOwner { get; set; }
public virtual ICollection<BusinessSubProcess> BusinessSubProcesses { get; set; }
public int BusinessLineID { get; set; }
[ForeignKey("BusinessLineID")]
public virtual BusinessLine BusinessLine { get; set; }
}
public class BusinessSubProcess
{
[Key]
public int BusinessSubProcessId { get; set; }
public string SubProcessName { get; set; }
public string SubProcessOwnerName { get; set; }
public DateTime Created { get; set; }
public DateTime LastChange { get; set; }
public int BusinessProcessID { get; set; }
[ForeignKey("BusinessProcessID")]
public virtual BusinessProcess BusinessProcess { get; set; }
}