我在使用实体框架生成的数据库时遇到问题。 在我的数据库中我想要以下数据: 包含测试请求的表和用户表。 每个用户可以是一个或多个测试请求的作者,但 1 个测试请求只能有 1 个作者。
我使用 Entity Framework Core .NET 命令行工具 7.0.11
这些是我的课程:
public class TestRequest
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public TestRequestStatus Status { get; set; }
public List<Signature> Signatures { get; set; }
public List<Device> Devices { get; set; }
public List<ItemNumber> ItemNumbers { get; set; }
public List<SerialNumber> SerialNumbers { get; set; }
public List<Norm> Norms { get; set; }
public List<User> Developers { get; set; }
public User Author { get; set; }
public ICollection<CommentaryModel> Comments { get; set; } = new List<CommentaryModel>();
}
public class User
{
public int Id { get; set; }
public string UserName { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
public AuthorizationStatus AuthorizationStatus { get; set; }
public string AuthorizationMessage { get; set; }
public User AuthorizationByUser { get; set; }
public Role Role { get; set; }
public List<TestRequest> TestRequests { get; set; }
}
在我的应用程序 DBContext 中:
. . .
public DbSet<TestRequest> TestRequests { get; set; }
public DbSet<User> Users { get; set; }
. . .
我期待一个没有任何有关测试请求的表用户。以及一个包含 FK 作者到用户 ID 的 TestRequests 表,或者将 TestRequests 与用户链接的第三个表。
但是我得到了这个:
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
UserName = table.Column<string>(type: "nvarchar(max)", nullable: false),
LastName = table.Column<string>(type: "nvarchar(max)", nullable: false),
FirstName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Email = table.Column<string>(type: "nvarchar(max)", nullable: false),
AuthorizationStatusId = table.Column<int>(type: "int", nullable: false),
AuthorizationMessage = table.Column<string>(type: "nvarchar(max)", nullable: false),
AuthorizationByUserId = table.Column<int>(type: "int", nullable: false),
RoleId = table.Column<int>(type: "int", nullable: false),
TestRequestId = table.Column<int>(type: "int", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
table.ForeignKey(
name: "FK_Users_AuthorizationStates_AuthorizationStatusId",
column: x => x.AuthorizationStatusId,
principalTable: "AuthorizationStates",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Users_Roles_RoleId",
column: x => x.RoleId,
principalTable: "Roles",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Users_TestRequests_TestRequestId",
column: x => x.TestRequestId,
principalTable: "TestRequests",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Users_Users_AuthorizationByUserId",
column: x => x.AuthorizationByUserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
(问题是 TestRequestId 作为表用户中的一列)
我多次尝试删除数据库、删除所有迁移、构建新数据库、清理所有... 我不知道我错过了什么。
你们知道为什么会发生这种情况吗?
EFCore 不知道 TestRequest 中的 Author 属性与用户拥有的 TestRequest 列表有任何关系。您可以使用 FluentAPI 告诉 EFCore。在 onModelCreating 中添加以下代码:
modelBuilder.Entity<TestRequest>()
.HasOne<User>(tr => tr.Author)
.WithMany(u => u.TestRequests);