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 [ 20751 ]
(4 Like)
4.82 (11 Vote)
Rate

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 dotnet-concept.com

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
Comment

User Comments


Van Tien : Thank u so much
September 29, 2021 at 10:56 PM
Ilia Shterev : StoreGeneratedPattern = "Computed" is good if you are never going to update that column. EF will not let you update. If you want it updateable I guess the option is Default value in the property table. Which I guess sends its own default values which have nothing do with DB column's default value.
January 06, 2021 at 7:02 PM
Anonymous : I had a problem with this. It writes over values I add when I make a new item. Like this: var city = new City(); city.IsActive = true; dbContext.Cities.Add(city); dbContext.SaveChanges(); // Now IsActive is Computed and overwritten. My solution: Create a new class: CityWithDefaultVaules.cs ... public partial class City { public City() { IsActive = false; } } ...
May 20, 2020 at 5:13 AM
Anonymous : Awesome man
October 18, 2018 at 1:00 PM
Steve Bez : Thanks, this was a huge help :)
April 29, 2017 at 12:41 AM
Anonymous : Helpful.... Also there is a separate case when the column in database table is also set as a Primary Key, in such case we need to set StoreGeneratedPattern as Identity for that property in edmx designer.
June 13, 2016 at 9:19 AM
 
 
 
 
 
 
Sponsors