我使用的是 Visual Studio Community 2022(64 位,版本 17.1.0 Preview 1.1)、ASP .Net 5 Razor Pages(非 MVC)、EF Core 5 和 SQL Server。
我有兩個類,它們具有多對多的關系。
第 1 類(標簽):
public class Tag
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name = "Tag Description")]
[StringLength(50, ErrorMessage = "Description cannot be longer than 50 characters.")]
public string Description { get; set; }
[Required]
public string Narrative { get; set; }
//Tags Have One Subject
//Tag.SubjectId FK
[ForeignKey("SubjectID")]
public int SubjectId { get; set; }
public Subject Subject { get; set; }
//Tags Have One Or More Documents
//(TagDocument Join Table)
public List<TagDocument> TagDocuments { get; set; }
//Tags Have One Or More Acronyms
//TagAcronym Join Table
public ICollection<Acronym> Acronyms { get; set; }
public List<TagAcronym> TagAcronyms { get; set; }
}
第 2 類(首字母縮寫詞):
public class Acronym
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name = "Acronym")]
[StringLength(50, ErrorMessage = "Acronym cannot be longer than 50 characters.")]
public string Abbreviation { get; set; }
[Required]
[Display(Name = "Description")]
[StringLength(100, ErrorMessage = "Description cannot be longer than 100 characters.")]
public string Description { get; set; }
[StringLength(250, ErrorMessage = "URL cannot be longer than 250 characters.")]
public string URL { get; set; }
//Acronyms Have One Or More AcronymOld
//AcronymOld.AcronymId (FK)
public List<AcronymOld> AcronymsOld { get; set; }
//Acronyms Have One Or More Tags
//TagAcronym Join Table
public ICollection<Tag> Tags { get; set; }
public List<TagAcronym> TagAcronyms { get; set; }
}
任何 Acronym 都可以有很多 Tag,而一個 Tag 可以有很多 Acronyms。
我創建了一個 TagAcronym 類來方便在資料庫中創建 Join Table:
public class TagAcronym
{
public int TagId { get; set; }
public Tag Tag { get; set; }
public int AcronymId { get; set; }
public Acronym Acronym { get; set; }
}
}
當我創建遷移時,由于我無法理解的原因,它嘗試創建一個 TagAcronym 和一個 AcronymTag 表。
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "AcronymTag",
columns: table => new
{
AcronymsId = table.Column<int>(type: "int", nullable: false),
TagsId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AcronymTag", x => new { x.AcronymsId, x.TagsId });
table.ForeignKey(
name: "FK_AcronymTag_Acronym_AcronymsId",
column: x => x.AcronymsId,
principalTable: "Acronym",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_AcronymTag_Tag_TagsId",
column: x => x.TagsId,
principalTable: "Tag",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "TagAcronym",
columns: table => new
{
TagId = table.Column<int>(type: "int", nullable: false),
AcronymId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_TagAcronym", x => new { x.TagId, x.AcronymId });
table.ForeignKey(
name: "FK_TagAcronym_Acronym_AcronymId",
column: x => x.AcronymId,
principalTable: "Acronym",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_TagAcronym_Tag_TagId",
column: x => x.TagId,
principalTable: "Tag",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_AcronymTag_TagsId",
table: "AcronymTag",
column: "TagsId");
migrationBuilder.CreateIndex(
name: "IX_TagAcronym_AcronymId",
table: "TagAcronym",
column: "AcronymId");
}
任何幫助表示贊賞。謝謝。
uj5u.com熱心網友回復:
Tag 和 Acronym 之間的關系是多對多的,因此 EF 會自動為這些具有名稱(TagAcronym 或 AcronymTag)的表創建映射表,因此您無需明確指定 TagAcroymn 表。
uj5u.com熱心網友回復:
您可以使用此模型設計:
public class Tag
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name = "Tag Description")]
[StringLength(50, ErrorMessage = "Description cannot be longer than 50 characters.")]
public string Description { get; set; }
[Required]
public string Narrative { get; set; }
//Tags Have One Subject
//Tag.SubjectId FK
[ForeignKey("SubjectID")]
public int SubjectId { get; set; }
public Subject Subject { get; set; }
//Tags Have One Or More Documents
//(TagDocument Join Table)
public List<TagDocument> TagDocuments { get; set; }
//Tags Have One Or More Acronyms
//TagAcronym Join Table
public List<TagAcronym> TagAcronyms { get; set; }
}
public class Acronym
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name = "Acronym")]
[StringLength(50, ErrorMessage = "Acronym cannot be longer than 50 characters.")]
public string Abbreviation { get; set; }
[Required]
[Display(Name = "Description")]
[StringLength(100, ErrorMessage = "Description cannot be longer than 100 characters.")]
public string Description { get; set; }
[StringLength(250, ErrorMessage = "URL cannot be longer than 250 characters.")]
public string URL { get; set; }
//Acronyms Have One Or More AcronymOld
//AcronymOld.AcronymId (FK)
public List<AcronymOld> AcronymsOld { get; set; }
//Acronyms Have One Or More Tags
//TagAcronym Join Table
public List<TagAcronym> TagAcronyms { get; set; }
}
資料背景關系
protected override void OnModelCreating(ModelBuilder modelBuilder) {
//Tag and Subject one-to-one
modelBuilder.Entity<Subject>()
.HasOne(b => b.Tag)
.WithOne(i => i.Subject)
.HasForeignKey<Tag>(b => b.SubjectId);
//Tag and TagDocument one-to-many
modelBuilder.Entity<Tag>()
.HasMany(t => t.TagDocuments)
.WithOne(g => g.Tag)
.HasForeignKey(g => g.TagId);
//Acronym and AcronymsOld one-to-many
modelBuilder.Entity<Acronym>()
.HasMany(t => t.AcronymsOld)
.WithOne(g => g.Acronym)
.HasForeignKey(g => g.AcronymId);
//Acronym and Tag many-to-many
modelBuilder.Entity<TagAcronym>()
.HasKey(t => new { t.TagId, t.AcronymId });
modelBuilder.Entity<TagAcronym>()
.HasOne(pt => pt.Tag)
.WithMany(p => p.TagAcronyms)
.HasForeignKey(pt => pt.TagId);
modelBuilder.Entity<TagAcronym>()
.HasOne(pt => pt.Acronym)
.WithMany(t => t.TagAcronyms)
.HasForeignKey(pt => pt.AcronymId);
}

可以參考官方檔案:https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api,fluent-api-simple-key,simple-key#many-太多
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/401360.html
上一篇:如何設定使用哪個服務?
