How to set default column value in EF? RRS feed

  • General discussion

  • I want to use the EF like a model design tool like Erwin. And design all my entity/relationship firstly, then generate database from model.

    The problem is that if we create a table by sql command, we can set column's default value. For example,

    CREATE TABLE [dbo].[Product](
    [EffectiveStartTime] [datetime] NOT NULL,
    [ProductId] [int] NOT NULL default(1),
    [ProductName] [nchar](10) NULL


    But how to do this in EF designer? I find out there is a property named "Default Value" for a column, but it's used to set

    the default value in a the constructor....  I try a lot and check the DDL which is generated from my model, the DDL

    will not contains the key word:default(). 

    PS: Also I try to set a column a default value in a test database, and update model from that database, and can't find any information for column default value in the edmax file.

    Any one can tell me how to fix this problem.

    • Edited by Klaus Zou Monday, November 25, 2013 10:27 AM
    Monday, November 25, 2013 10:26 AM

All replies

  • Hi,

    I don't think it's possible using a desing model.

    As you said, you can set this kind of values in the default constructor (without parameters) of a partial class, for example:

    public partial class Product
       public Product()
          this.ProductId = 1;

    You can also set this value in model when it is an Identity property:

    This same approach, could be used also with code-first:


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    • Edited by JA Reyes Monday, November 25, 2013 10:47 AM syntax
    Monday, November 25, 2013 10:46 AM
  • Hello,

    I think we do not need to take care of the created DLL, whether it has a default value or not.

    If we set the property “Default Value”, we will get the same effect if we set a default value in DLL.

    There is no difference actually. And we can see the default value by open the edmx file using text editor and find the property, then we will see it in both CSDL and SSDL sections.


    <EntityType Name="Markets">
          <PropertyRef Name="MarketID" />
        <Property Name="MarketID" Type="int" Nullable="false" />
        <Property Name="MarketName" Type="nvarchar(max)" Nullable="false" DefaultValue="Name001" />


    <EntityType Name="Markets"> <Key> <PropertyRef Name="MarketID" /> </Key> <Property Type="Int32" Name="MarketID" Nullable="false" annotation:StoreGeneratedPattern="None" /> <Property Type="String" Name="MarketName" Nullable="false" MaxLength="Max" Unicode="true" FixedLength="false" DefaultValue="Name001" /> <NavigationProperty Name="Properties" Relationship="S09.MarketsProperties" FromRole="Markets" ToRole="Properties" /> </EntityType>


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 26, 2013 3:30 AM