Even if there is no NULL value, Entity Framework CTP 4 says "Cannot enter the value NULL into column."

c# ctp4 entity-framework


I employ EF CTP 4. For testing reasons, I have a simple console application that uses EF to put some data into a SQL database.

I've encountered a situation where, after adding the item,

using(var context = GetContext())
   BOB b = new BOB();
   b.Id = 1;


The error code is { "Cannot enter NULL into column "Id" of table "TestDB.dbo.BOB" because that column does not support null values. failure of INSERT The statement has come to an end." }.

The table just has one field, "Id int NOT NULL", which serves as both the primary key and an automatically incrementing ID.

I have this configuration, which is triggered when the DataContext is created.

protected override void OnModelCreating(ModelBuilder builder)
    builder.Entity<BOB>().HasKey(b => b.Id);

I've also pre-populated this table, and using the debugger, I was able to load up this BOB object using the command watch. I'm really at a loss as to why this happens since loading up my BOB indicates that everything is in order, but when I try to enter a new one, it crashes.

1/12/2014 3:54:29 PM

Accepted Answer

Have you ever attempted mentioning theStoreGeneratedPattern ?

    .HasKey(p => p.Id)
        .Property(p => p.Id)
            .StoreGeneratedPattern = StoreGeneratedPattern.None;

12/15/2010 9:48:52 AM

Popular Answer

I'm experiencing the same problem, and the remedy is somewhat nasty.

public Int64 PolicyID { get; set; }

This number was NOT produced automatically.

The identical mistake then occurred.

First CTP5 for EF Code

after using this

 public Int64 PolicyID { get; set; }

then it will function.

Related Questions


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