Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework CTP 4. "Cannot insert the value NULL into column" - Even though there is no NULL value

Im using EF CTP 4. I have a simple console app (for testing purposes) that is using EF to insert some data into a SQL database.

I have come to a problem where by upon inserting the item

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

It throws the error: {"Cannot insert the value NULL into column 'Id', table 'TestDB.dbo.BOB'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

The Table just has 1 field of Id int NOT NULL which is the primary key and is not an auto incremented Id.

On the creation of the DataContext I have this configuration, which yes does get fired.

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

I have also pre-populated this table and then through the debugger been able to via watch load up this BOB object... so I am really stumped, as for being able to load up my BOB shows that all is right... however upon inserting a new one it crashes...

like image 345
Secret Squirrel Avatar asked Dec 14 '10 21:12

Secret Squirrel


1 Answers

i have the same issue here and it's really an ugly solution.

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

this is NOT an auto generated number

then i hit the same error.

EF Code First CTP5

after apply this:

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

then it will work.

like image 151
sitem00n Avatar answered Sep 29 '22 11:09

sitem00n