Reputation: 7555
I am struggling to solve this issue, despite there being many threads about it on SO.
I have a class that has a nullable DateTime?
field like this:
public class ActiveItem
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public Nullable<DateTime> TimeStarted { get; set; }
public Nullable<DateTime> LastHeard { get; set; }
}
I have a database schema defined as:
CREATE TABLE [dbo].[ActiveItems] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[TimeStarted] DATETIME NULL,
[LastHeard] DATETIME NULL,
CONSTRAINT [PK_ActiveWorkflows] PRIMARY KEY CLUSTERED ([ID] ASC)
);
I then try to save an ActiveItem
where TimeStarted
and LastHeard
are null. However, when I try to do so I get the following error:
"Cannot insert the value NULL into column 'TimeStarted', table 'dbo.ActiveItems'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."
I can't seem to get around this.
The odd thing is that I can login to the SQL server and create a row in the table with null values. Is this entity framework screwing up?
Upvotes: 3
Views: 1303
Reputation: 15685
To ensure that you're hitting the correct database, do a SQL Trace using Profiler.
It's likely if the constraint violation is inconsistent with what you expect (in terms of the schema) that you're hitting a different database.
Following that, update the connection string used to connect to Entity Framework to the correct database.
Upvotes: 1