Please enable Javascript for better experience...
Inserting default values in database while inserting record in Entity Framework
By Rahul Kumar Jha | Jan 3, 2016 | In Tips | Total Views [ 19868 ]
(3 Like)

You can set "StoredGeneratedProperty" attribute of table column in the EDMX file to Computed to enable default value insertion in entity framework.

The Problem

While working with Entity Framework, you would have noticed that it does not insert default value into the database. You need to manually set values for those properties which have already a default value set in database table. This seems as an extra work to set values for default columns. Suppose, you are saving created date of a row in your table and for this you are using default constraint as today's days. Now when you insert a new record in database, it works absolutely fine but if you perform this operation using entity framework, it will not let you doing so. To insert default value, you need to manually assign a value to that column.

The Solution

If you are using code first approach, you can set default values using [DefaultValue()]  attribute or if you are using database first and using edmx file then you can set StoreGeneratedPattern = "Computed" of that particular column. Just follow the steps below.

Step 1: Open EDMX file and locate your table in that file.

step 1

Step 2: Select the property name. Right click and click on property.

step 2

Step 3: You will find a property named as StoreGeneratedPattern with value set as None.

step 3

Step 4: Set StoreGeneratedPattern = "Computed".

step 4

And you are done. Now you can insert default values using EF. Hope this helps you. Pleas like and rate the article. You can comment your views below.

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer

Public profile: user/profile/99900001

Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments

Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID