0

我有 2 个实体:

public class User
{
  public int userId { get; set; }
  public string name { get; set; }
  public Guid userGuid { get; set; }
}

public class Absence
{
  public int absenceId { get; set; }
  public Guid applicantId { get; set; }
  public User applicant { get; set; }
  public Guid permitterId{ get; set; }
  public User permitter{ get; set; }
  ...
}

AbsencesConfiguration:
  this.HasRequired(u => u.Applicant).WithMany().HasForeignKey(d => d.ApplicantId);
  this.HasRequired(u => u.Permitter).WithMany().HasForeignKey(d => d.PermitterId);

我想要两个类之间的 Fluent API 映射,但它给出了以下错误消息:

Blockquote\tSystem.Data.Entity.Edm.EdmAssociationConstraint: : 引用约束的 Dependent Role 中所有属性的类型必须与 Principal Role 中对应的属性类型相同。实体“Absences”上的属性“ApplicantId”类型与引用约束“Absences_Applicant”中实体“User”上的属性“UserId”类型不匹配。

我认为这是因为 EF 尝试使用 User 实体的 UserId 而不是 UserGuid 列加入这两个表。我以为我会让这两列 Absence 实体独一无二,但我如何将它们映射在一起?

提前致谢。

4

1 回答 1

5

问题是您的用户主键是 int,但您的外键是 Guid。

您需要更改您的 User 类以获得 userId 的 guid:

  public Guid userId { get; set; }

或者,更新您的 Absence 类以使用 int:

 public int applicantId { get; set; }
于 2012-09-05T12:39:13.500 回答