2017-04-05 43 views
0

我正在使用实体框架来构建我的数据库。我的模型包含两个实体:EntiteApplicationUser(请参阅下面的代码)。实体框架错误:无法确定类型之间的关联主体端

有这些实体之间的两个关系:

  1. 一到多:一个Entite可以包含一个或多个用户。并且用户属于一个Entite
  2. 一对一:一个Entite必须有一个用户作为负责人和一个用户可以只负责一个Entite

Entite

public class Entite : AuditableEntity<int> 
{ 
     [Required] 
     [MaxLength(10)] 
     public String code { get; set; } 

     [Required] 
     [MaxLength(50)] 
     public String Libelle { get; set; } 

     [Required] 
     [MaxLength(10)] 
     public String type { get; set; } 

     [Key, ForeignKey("ResponsableId")] 
     public int? ResponsableId { get; set; } 

     public virtual ApplicationUser responsable { get; set; } 
     public int? RattachementEntiteId { get; set; } 
     [Key, ForeignKey("RattachementEntiteId")] 
     public virtual Entite rattachement { get; set; } 

     public List<Entite> Children { get; set; } 
} 

ApplicationUser

public class ApplicationUser : IdentityUser 
{ 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public int Matricule { get; set; } 
     public DateTime? dateRecrutement { get; set; }  
     public int? entiteId { get; set; } 
     [ForeignKey("entiteId")] 
     public virtual Entite entite { get; set; } 
} 

当我尝试使用Add-Migration命令来构建数据库,我得到这个错误:

无法确定类型之间关联的主要端点

有关此问题的任何想法?

感谢您的帮助

回答

0

它看起来像在您的Entite模型类中的小错字/错误。

ForeignKey应引用您的ApplicationUser,当前它正在引用自身,并且将为responsable生成一个新密钥。

如果我们交换ForeignKey参考下面,那么这个看起来应该解决您的问题:

[Key, ForeignKey("responsable")] 
public int? ResponsableId { get; set; } 

public virtual ApplicationUser responsable { get; set; } 

或者你可以交换引用就像你有你的rattachement完成。

public int? ResponsableId { get; set; } 
[Key, ForeignKey("ResponsableId ")] 
public virtual ApplicationUser responsable { get; set; } 
相关问题