Skip to content Skip to sidebar Skip to footer

Cannot Insert The Value Null Into Column ... When The Value Is Not Null

in my c# code I have an insert into my DB, but it throws an exception Cannot insert the value NULL into column 'BoxID', table 'Moroccanoil_Replicated.dbo.Boxes'; column does not

Solution 1:

As mentioned in comments:

Most likely EF won't pass the ID parameter because it thinks the value is created by the database. This means you should turn off DatabaseGeneratedOptions.Identity for this column.

With DatabaseGeneratedOption.Identity, EF won't pass the ID value to the database because it suspects it to be given by DBMS. This can lead to this issue or an DbUpdateConcurrencyException, when it suspects an object with a given Id value to be in the database, but it was actually altered by DBMS.

Solution 2:

Like mentioned, EF does not pass ID column to the database. If you have a case that you don't want to have auto increment value, below is an example

publicclassPackingInventory
{

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    publicint ID { get; set; }

Post a Comment for "Cannot Insert The Value Null Into Column ... When The Value Is Not Null"