简体   繁体   中英

Entity Framework - Cannot insert the value NULL into column?

I have looked at all the similar questions on this topic but none seem to work for me. I don't know exactly why I am getting the below error when I try to run these two commands: add-migration and then update-database .

Cannot insert the value NULL into column 'StudentId', table 'aspnet-JavaWebsiteProject-20171006053028.dbo.Groups'; column does not allow nulls. UPDATE fails. The statement has been terminated.

I am using Entity Framework v6.2.0 . I have three simple models below:

public class Student
{
    public int Id { get; set; }

    public string UserId { get; set; }

    [EmailAddress]
    public string Email { get; set; }
}

Teacher model:

public class Teacher
{
        public int Id { get; set; }

        public string UserId { get; set; }

        [EmailAddress]
        public string Email { get; set; }
    }

//Group model:

public class Group
{
    public int Id { get; set; }

    [Required]
    public int StudentId { get; set; }
    public Student Student { get; set; }

    [Required]
    public int TeacherId { get; set; }
    public Teacher Teacher { get; set; }
} 

Can anyone guide me on what I might be doing wrong or how I can fix this error?

Thank you

If you want the column to be nullable, you should set the property of your model to a nullable type.

public int? StudentId { get; set; }

After this, you will have to create and run your migrations again.

Relating @StefanW.'s comment, if the Groups table already exists and you are adding the StudentId (and other) column(s) to it, if there are existing rows then those rows will have to have some value specified for each of the new columns. If you haven't specified a default value for the column then the database will attempt to use NULL , which will fail because the column does not allow null values.

This question has been around for years. There are dozens of topics about it. In most topics, the answer is "put IsIdentity=true and specify Identity start and step in the table design" , or put your identity definition in SQL,

 CREATE TABLE ..
     [id] INT IDENTITY(1,1) NOT NULL
     ...

like in

Cannot insert the value NULL into column 'Id', Entity framework

.. and for some 75% of members, that answer works immediately ! But.. for the 25% it seems to be no solution. There are topics with an acknowledged answer, that suggest to recreate the table involved using migration commands (edmx context),

Entity Framework Cannot insert the value NULL into column Identity Specification set to No

.. but I did not want to recreate my precious table, so that procedure was no option. Today I solved it here, without having to reset any tables. And the cause was very trivial. My connection string did not have a database addressed, so I actually scaffolded my tables in another database on the same server, where IDENTITY[1,1] was not set in Table create ! Of course, while debugging I checked only my own database :) , which was set correctly. But measures in table design had no effect. Of course not.. I was editing the wrong database !

To solve it, I changed my connection string to include initial catalog, from

 <add name="MyDBContext"  connectionString="Data Source=..;Persist Security Info=True;User ID=..;Password=.."  providerName="System.Data.SqlClient" />

to

 <add name="MyDBContext"  connectionString="Data Source=..;Persist Security Info=True;initial catalog=MyDatabase;User ID=..;Password=.."  providerName="System.Data.SqlClient" />

One question remains.. why does Microsoft not require us to set the initial catalog for Framework 6 connection strings in app.config ? Above procedure could have destroyed an existing other database without notice..

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