0

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

5
  • 2
    is it initial migration or do you have already the tables in your data base? Commented Feb 16, 2018 at 15:41
  • it is initial migration - the very first one Commented Feb 16, 2018 at 16:06
  • try to dump you database and run: Add-Migration InitialCreate –IgnoreChanges Commented Feb 16, 2018 at 16:17
  • It's been a while since I've used migrations so I forget the exact mechanics, but is your migration creating initial seed data in the database? And if so, is that data defined completely for the Groups table? Commented Feb 16, 2018 at 16:18
  • @StefanW. Do you know what the problem could be, if I get this error when trying to alter already existing column's datatype? Commented Dec 9, 2021 at 8:39

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.