Default Value & CreateDatabaseScript

Topics: General
Sep 24, 2013 at 11:28 AM
Hi,
i noticed that default values in ssdl property element is not taken into account when generating the script for database creation CreateDatabaseScript.

For example starting with this entity (ssdl)
  <EntityType Name="Entity2Set">
    <Key>
      <PropertyRef Name="Id" />
    </Key>
    <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
    <Property Name="Value" Type="nvarchar(max)" Nullable="true" DefaultValue="1" />
  </EntityType>
I would expect a create table sql statement looking like this
-- Creating table 'Entity2Set'
CREATE TABLE [dbo].[Entity2Set] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Value] nvarchar(max)  NULL default '1'
);
GO
What EF generates is:
-- Creating table 'Entity2Set'
CREATE TABLE [dbo].[Entity2Set] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Value] nvarchar(max)  NULL
);
GO
Note there isn't the default value on column [value].
Is this the expected behavior?

P.S. This behavior has been verified on both EF5 & EF6.

Regards, Max
Sep 24, 2013 at 3:44 PM
Hi Max,

Currently the DefaultValue from the SSDL is not being used to generate the database script (also note that in the model first scenario you may not even have the SSDL). This value is used at runtime if you happen to have a non-nullable column in the database/SSDL that is not mapped in your model. In this case if there was not for the DefaultValue all inserts would fail since EF would not know what value to use for the non-mapped column since nulls are not allowed.

Pawel
Sep 27, 2013 at 8:08 AM
Got it.

Thank you Pawel.