简体   繁体   中英

Entity Framework Core Migrationbuilder not setting nullable property on foreign key

Entity Framework Core Migrationbuilder not setting nullable property on foreign key

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?

etc....

fluent code..

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();

This is de missing fluent code. I don't have a combined primary key anywhere in any classes.

The Project class: public partial class Project {

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

etc...

It is somehow part of your primary key which by definition cannot contain nulls. I have a feeling that you have not shown complete information here.

Sorry My bad. I discovered that I created a many-to-many relationship in fluent code, which was actually supposed to be on a one-to-many relationship in fluent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM