JohnyL
JohnyL

Reputation: 1

Value for PRIMARY KEY recognized as NULL (EF6 Code First)

Well, I have rather odd exception when I try to insert new value. Please, see attached files for further investigation. So, I use:

The problem is I'm trying to insert new value into simple table called dbo.Worker2 (see "Worker2 Table" image file) which consists of 2 columns: TableNumber and Name. The TableNumber column is primary key of integer data type (see "Primary Key column properties" image file). I have installed the latest EF6 alpha3 and trying to insert new value (see "VB.NET Code" image file). However, exception is thrown: "Cannot insert NULL the value NULL into column 'TableNumber', table Bonus.dbo.Worker2'; column does not allow nulls. INSERT fails." (see "VS Exception" image file).

Then I have decided to see what T-SQL query EF sends to SQL Server with SQL Server Profiler and was surprised that no TableNumber was sent by EF (see "Profiler Queries" image file).

Is it bug or I have missed something?

Upvotes: 0

Views: 997

Answers (2)

Gary Doublé
Gary Doublé

Reputation: 446

By the looks of it you need to set your Worker2 Primary Key property as non-database generated.

something like:

<Key, DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property TableNumber As Integer

Upvotes: 3

amitabha
amitabha

Reputation: 646

I think Primary Key is not set to identity.

Upvotes: 0

Related Questions