EF Core 使用 Include Join 但 ForeignKey 不是另一个 table 上的主键
EF Core Join using Include but ForeignKey is not Primary Key on the other table
我正在尝试将我的 Table 与另一端的 ForeignKey 和 PrimaryKey 关联起来。但是现在我将使用一个外键,它不是上述 table 的主键。我正在使用 [InverseProperty] 但我认为它有一个错误,因为我已经四处寻找了几个小时,他们都说了同样的事情。
文件 Table:
public class Document
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DocumentId { get; set; }
public int ProjectId { get; set; }
public int DepartmentId { get; set; }
public int AuthorId { get; set; }
[NotMapped]
public virtual User Author { get; set; }
}
用户
public class User
{
[Key]
public int UserId { get; set; }
public int AuthUserId { get; set; }
public string DisplayName { get; set; }
[NotMapped]
[ForeignKey("AuthorId")]
public virtual Document Document { get; set; }
}
上下文:
modelBuilder.Entity<User>(entity =>
{
entity.HasOne(u => u.Document)
.WithMany("AuthorId");
});
我正在尝试使用他们 的解决方案,但没有成功。
任何帮助将不胜感激。谢谢!
But now i will be using a ForeignKey which is not the primary for the said table.
为此,您可以使用 EF Core Alternate Keys 功能。但首先更正你的模型 class 设置如下:(如你所说,一个 User
将有多个 Document
)
public class Document
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DocumentId { get; set; }
public int ProjectId { get; set; }
public int DepartmentId { get; set; }
public int AuthorId { get; set; }
public User Author { get; set; }
}
public class User
{
[Key]
public int UserId { get; set; }
public int AuthUserId { get; set; }
public string DisplayName { get; set; }
public ICollection<Document> Documents { get; set; }
}
然后在Fluent API
配置如下:
modelBuilder.Entity<Document>()
.HasOne(p => p.Author)
.WithMany(b => b.Documents)
.HasForeignKey(p => p.AuthorId)
.HasPrincipalKey(b => b.AuthUserId); // <-- here you are specifying `AuthUserId` as `PrincipalKey` in the relation which is not primary key
我正在尝试将我的 Table 与另一端的 ForeignKey 和 PrimaryKey 关联起来。但是现在我将使用一个外键,它不是上述 table 的主键。我正在使用 [InverseProperty] 但我认为它有一个错误,因为我已经四处寻找了几个小时,他们都说了同样的事情。
文件 Table:
public class Document
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DocumentId { get; set; }
public int ProjectId { get; set; }
public int DepartmentId { get; set; }
public int AuthorId { get; set; }
[NotMapped]
public virtual User Author { get; set; }
}
用户
public class User
{
[Key]
public int UserId { get; set; }
public int AuthUserId { get; set; }
public string DisplayName { get; set; }
[NotMapped]
[ForeignKey("AuthorId")]
public virtual Document Document { get; set; }
}
上下文:
modelBuilder.Entity<User>(entity =>
{
entity.HasOne(u => u.Document)
.WithMany("AuthorId");
});
我正在尝试使用他们
任何帮助将不胜感激。谢谢!
But now i will be using a ForeignKey which is not the primary for the said table.
为此,您可以使用 EF Core Alternate Keys 功能。但首先更正你的模型 class 设置如下:(如你所说,一个 User
将有多个 Document
)
public class Document
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DocumentId { get; set; }
public int ProjectId { get; set; }
public int DepartmentId { get; set; }
public int AuthorId { get; set; }
public User Author { get; set; }
}
public class User
{
[Key]
public int UserId { get; set; }
public int AuthUserId { get; set; }
public string DisplayName { get; set; }
public ICollection<Document> Documents { get; set; }
}
然后在Fluent API
配置如下:
modelBuilder.Entity<Document>()
.HasOne(p => p.Author)
.WithMany(b => b.Documents)
.HasForeignKey(p => p.AuthorId)
.HasPrincipalKey(b => b.AuthUserId); // <-- here you are specifying `AuthUserId` as `PrincipalKey` in the relation which is not primary key