I am getting the following error:
InnerException: {“Cannot insert the value NULL into column ‘UserId’, table ‘database.dbo.bp_UserDetails’; column does not allow nulls. INSERT fails. The statement has been terminated.”}
The problem is that UserId has a value of 12.
My table is defined as
CREATE TABLE [dbo].[bp_UserDetails](
[UserId] [int] NOT NULL,
[Name] [varchar](50) NULL.
// Other fields
CONSTRAINT [PK_bp_UserDetails] PRIMARY KEY CLUSTERED
(
[UserId] ASC
)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
My model:
[Table("bp_UserDetails")]
public class UserDetails
{
[Key]
public int UserId { get; set; }
[DisplayName("Customer name")]
public string Name { get; set; }
}
Any ideas why I may be getting this error?
Set
DatabaseGeneratedOptiontoNonefor the key property in your model:If you don’t do this EF assumes by default that the
UserIdis generated in the database as an identity column and never sends the value to the server.