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

c# ctp4 entity-framework

Question

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;

   context.Bobs.Add(b);
   context.SaveChanges();
}

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);
    builder.Entity<BOB>().MapSingleType().ToTable("BOB");
}

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
53
1/12/2014 3:54:29 PM

Accepted Answer

Have you ever attempted mentioning theStoreGeneratedPattern ?

modelBuilder.Entity<BOB>()
    .HasKey(p => p.Id)
        .Property(p => p.Id)
            .StoreGeneratedPattern = StoreGeneratedPattern.None;

builder.Entity<BOB>().MapSingleType().ToTable("BOB");
29
12/15/2010 9:48:52 AM

Popular Answer

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

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

This number was NOT produced automatically.

The identical mistake then occurred.

First CTP5 for EF Code

after using this

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

then it will function.



Related Questions





Related

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