Reputation: 13083
I am using EFv4 for my ORM and I just tried to insert a record into a table that has multiple columns marked as not nullable and with a default value of empty string (for nvarchar
type columns). The problem is that EF does not pick up default values for this column and when I create an instance of my Entity but leave some string properties null
and then try to save this in DB table an exception occurs (saying I should relax my constraint settings on dataset).
So while the default value of empty string works from within SQL Server Management Studio (if I leave column emtpy it will insert emtpy string automatically) it does not work from within EF.
I must say I actually noticed this before with uniqueidentifier
primary key columns but now I see that no information about column's defaults makes it into the EF model no matter what the column is.
Anyone else noticed this? any workarounds?
Also, if anyone know, are there any improvements around this in the latest EF Feature CTPs?
Upvotes: 1
Views: 222
Reputation: 46859
I have found that the default values that I assign when I create my tables in Management Studio, do not make it into the EF model when I do a "create model from database", but if you set the default value in SQL server, and then ALSO set the default value in the EF model, then when I create objects, they come with those default values set in the object....have you tried that?
Edit: For strings you need to edit the EDMX file directly like this:
<Property Type="String" Name="Listing" MaxLength="Max" FixedLength="false" Unicode="false" DefaultValue="" />
Upvotes: 0
Reputation: 48432
I wouldn't view this as a EF problem. L2S does the same thing. The 'problem' is that when you insert a row into your table, EF / L2S is actually including the column, in the Insert statement, with a NULL value. When an insert is done like this, your default values are not used.
You may have to do what we've done in our DAL and that is to replace the nulls with default values before doing the insert.
Upvotes: 3