Entity Framework Core Migrationbuilder not setting nullable property on foreign key

c# entity-framework-core

Question

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...

1
0
8/1/2017 2:55:46 PM

Accepted Answer

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.

0
8/1/2017 2:28:18 PM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow