Entity Framework CTP 4. "Cannot insert null value in column" - even though there is no null value

I use EF CTP 4. I have a simple console application (for testing) that uses EF to insert some data into an SQL database.

I ran into a problem when inserting an element

using(var context = GetContext()) { BOB b = new BOB(); b.Id = 1; context.Bobs.Add(b); context.SaveChanges(); } 

It throws an error: {"Cannot insert a NULL value in the Identifier column, table" TestDB.dbo.BOB "; the column does not allow nulls. INSERT does not work. \ R \ nApplication is complete." }

In the table there is only 1 field Id int NOT NULL, which is the primary key, and not automatically increasing identifier.

When creating a DataContext, I have this configuration, which yes works.

 protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<BOB>().HasKey(b => b.Id); builder.Entity<BOB>().MapSingleType().ToTable("BOB"); } 

I also pre-populated this table, and then through the debugger I was able to view the clock loading this BOB object ... so I'm actually a dead end, since in order to load my BOB, everything is correct ... however, when inserting a new one crash ...

+50
c # entity-framework ctp4
Dec 14 '10 at 21:26
source share
9 answers

Have you tried explicitly specifying StoreGeneratedPattern ?

 modelBuilder.Entity<BOB>() .HasKey(p => p.Id) .Property(p => p.Id) .StoreGeneratedPattern = StoreGeneratedPattern.None; builder.Entity<BOB>().MapSingleType().ToTable("BOB"); 
+28
Dec 15 '10 at 9:48
source share

I have the same problem and this is a really ugly solution.

  [Key] public Int64 PolicyID { get; set; } 

this is NOT an automatic number

then i get the same error.

EF Code First CTP5

after applying this:

  [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public Int64 PolicyID { get; set; } 

then it will work.

+69
Feb 24 2018-11-11T00:
source share

I am using EF 4.1, Model First and ran into this problem. Here's how I solved it:

When using the Model Designer surface, when you create the Entity, you need to define the "Key" property, by default it is Id, int32.

In my situation, I decided to use Guids for Id, so I would switch int32 to Guid. But if you check this identifier after creating the object, I saw that the identifier "StoreGeneratedPattern" has an "identifier". At first I did not think that this was a problem, but when I learned the SQL that was used to insert into the database, it was a bit strange, because it did not send my identifier. Disruption!

But as soon as I came back and changed "StoreGeneratedPattern" from "identity" to "none", restored the db and rebuilt the project, this weird {"Cannot insert a NULL value in the" Id "column, the table" TestDB ". Dbo.BOB '; column does not allow zeros. INSERT fails. \ r \ nApplication completed. " } stopped.

FYI - when viewing sql, it still seems a little that if you have an "identifier" selected for "StoreGeneratedPattern", EF saves the object in db (sans Id) and then immediately returns the identifier and saves it back to your object. those. this choice for "StoreGeneratedPattern" is based on db to generate your identifier, NOT your code!

+26
Mar 17 '11 at 11:29
source share

This happened to me when a primary key was missing from the reputable column (identification column) in the db schema. I exported data between SQL servers using the SSMS Export tool and creating a new database, but did not realize that it only exports data without keys.

+3
May 08 '13 at 9:19
source share

Could you post your C # code for type BOB? In particular, what is the type of property โ€œIdโ€? By convention, Code First will do any proeprty as Id or TypeName + Id, which is also an "int" identifier. However, it is not clear to me why in this case you will get an exception, so I would like to see the definition of your class.

According to your other questions, using multiple DbContext types in the same project is fully supported and should work fine with CTP4 and CTP5.

+1
Dec 15 '10 at 19:21
source share

You can also use

 modelBuilder.Entity<BOB>() .HasKey(p => p.Id) .Property(p => p.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); builder.Entity<BOB>().MapSingleType().ToTable("BOB"); 
+1
Sep 13 '18 at 19:37
source share

I know that this question is somehow outdated, and a decision has already been found, but I thought it would be useful if I shared my conclusions.

I had this error today because I used two different instances of the same DataContext . I created a new model with some properties - the values โ€‹โ€‹for these properties were loaded from the database using one instance of the DataContext , and then I tried to push this newly created model to the database, calling Add() first and then SaveChanges() in another instance DataContext . After I started using the same instance, both for getting property values โ€‹โ€‹and for adding and saving a new object, it all started.

0
Nov 07 '17 at 16:44
source share

I had a similar situation, but in my case even off Identity did not help.

The problem was related to the Primary Key, which I skipped to add entities to my model.

Here is the script that the model generated:

  CREATE TABLE [im].[SomeGroup] ( [Id] INT NOT NULL IDENTITY(1,1), -- this is mandatory for EF [OtherGroupId] INT NOT NULL, [Title] NVARCHAR(512) NOT NULL ) 

C # code for above:

 Insert(new SomeGroup { // I'm not providing Id here, cause it will be auto-generated SomeGroupId = otherGroup.Id, Title = otherGroup.Title }); 

Here is also some explanation for this.

0
Apr 02 '18 at 21:45
source share

If you use the database approach first, then first remove the corresponding object from the edmx diagram, and then update the model from the database, this will definitely solve your problem

0
Jul 21 '19 at 15:14
source share



All Articles