0
实体框架的核心Migrationbuilder设置为空的属性外键实体框架的核心Migrationbuilder不是外键
migrationBuilder.CreateTable(
name: "StudentProjects",
columns: table => new
{
StudentId = table.Column<int>(nullable: false),
**ProjectId** = table.Column<int>(nullable: false), // ??????? Why not setting nullable in migrationbuilder?
Active = table.Column<bool>(nullable: false),
Completed = table.Column<bool>(nullable: false),
EindDatum = table.Column<DateTime>(nullable: false),
Guid = table.Column<string>(nullable: false),
StartDatum = table.Column<DateTime>(nullable: false),
StudentProjectId = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_StudentProjects", x => new { x.StudentId, x.ProjectId });
table.UniqueConstraint("AK_StudentProjects_StudentProjectId", x => x.StudentProjectId);
table.ForeignKey(
name: "FK_StudentProjects_Projecten_ProjectId",
column: x => x.ProjectId,
principalTable: "Projecten",
principalColumn: "ProjectId",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "FK_StudentProjects_Studenten_StudentId",
column: x => x.StudentId,
principalTable: "Studenten",
principalColumn: "StudentId",
onDelete: ReferentialAction.SetNull);
});
[Table("StudentProjects")]
public partial class StudentProject
{
public StudentProject()
{
}
[Key]
public int StudentProjectId { get; set; }
public DateTime StartDatum { get; set; }
public DateTime EindDatum { get; set; }
public bool Active { get; set; }
public int? ProjectId { get; set; } // ??????? Why not setting nullable in migrationbuilder?
等....
流利的代码不为空的设置属性..
modelBuilder.Entity<StudentProject>()
.HasKey(bs => new { bs.StudentId, bs.ProjectId });
modelBuilder.Entity<StudentProject>()
.HasOne(bs => bs.Student)
.WithMany(b => b.StudentProjects)
.OnDelete(DeleteBehavior.SetNull)
.HasForeignKey(bs => bs.StudentId)
.IsRequired();
modelBuilder.Entity<StudentProject>()
.HasOne(bs => bs.Project)
.WithMany(s => s.StudentProjects)
.OnDelete(DeleteBehavior.SetNull)
.HasForeignKey(bs => bs.ProjectId)
.IsRequired();
这是缺少流利的代码。 我没有任何类中任何地方的组合主键。
项目类: 公共部分类项目 {
public Project()
{
StudentProjects = new List<StudentProject>();
ProjectSkills = new List<ProjectSkill>();
ProjectVakken = new List<ProjectVak>();
}
[Key]
public int ProjectId { get; set; }
[Required]
public string Naam { get; set; }
等等
请显示您使用的导航属性(两端)以及可能的数据注释和/或流畅的api配置。 – grek40
我刚刚添加了流利的代码。 – Tryan18
那么它已经被回答......'modelBuilder.Entity()。HasKey(bs => new {bs.StudentId,bs.ProjectId});' - 不要问为什么你的主键的一部分是不可空。 –
grek40