实体框架核心迁移构建器不在外键上设置可空属性

时间:2017-08-01 14:17:38

标签: c# entity-framework-core

实体框架核心迁移构建器未在外键上设置可空属性

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; }

等...

2 个答案:

答案 0 :(得分:0)

它在某种程度上是您的主键的一部分,根据定义,它不能包含空值。我觉得你没有在这里显示完整的信息。

答案 1 :(得分:0)

抱歉,我的不好。 我发现我用流利的代码创建了一个多对多的关系,实际上它应该是流利的一对多关系。