How to use default column value from database in Entity Framework?

I have a Date column in a table that has a default or binding like getutcdate (). I want to use this in the EDM generation framework.On entity, I was able to find the "Default value" property at the column level, but I think it is for a hard-coded value.

Please let me know how I can use the default value specified in the database.

+41
entity-framework
Feb 25 '09 at 3:02
source share
8 answers

Implementing the OnCreated event for an object is the solution I found. I had a Guid property that I wanted to populate. By default, it was filled with all zeros (00000-0000-00000, etc.). By adding the following to my partial object class, I was able to solve the problem.

partial void OnCreated() { Guid = Guid.NewGuid(); } 
+9
Oct 28 '10 at 22:20
source share

StoreGeneratedPattern = "Computed" is not the same as the default value because this property will be updated at any time with the default value in the database .. this means that it cannot be updated manually.

+4
Jun 23 '09 at 19:03
source share

You can set StoreGeneratedPattern to Identity, in which case EF reads the value returned from the database after executing the INSERT statement. The problem with this approach is that the next time you create an XML mapping, your changes will be lost.

Another way to do this is to set the value yourself in your DateTime.UtcNow code. You can set this in your entity constructor (if necessary, define a new constructor), or you can set it in your own event handler for your context. SavingChanges event (see How-to. Running Business Logic While Saving Changes (Entity Framework) for an example of handling the SavingChanges event).

+4
Jun 24 '09 at 10:33
source share

The problem with setting StoreGeneratedPattern = "Computed" or "Identity" is that they prevent the client from ever providing a value. Running this issue on inserts as well as updates.

It seems that StoreGeneratedPattern needs another parameter or two, so the database can at least see the values ​​provided by the user, but redefine them if necessary. This will allow you to use any existing DB insert or update triggers that update one field based on another field to work with. For example, a DB trigger for updating can update a modified timestamp only if it is not specified, and only if some fields have been updated.

Perhaps the advanced column attribute function in SQL Server can be used to automatically detect this field during extraction, so we are not editing XML files.

+4
May 26 '11 at 22:18
source share

In this post, another solution is proposed using the partial class and method in the constructor to set values.

How to use standard Entity Framework entities and default date values

+1
Nov 18 '12 at 10:16
source share

Here's a possible, but not very workaround -

The Computed in the column setting make it read-only, but will do the default value. It is possible to have a real computed column, for example, "LastChangedAt_computed", which either shows the value "LastChangedAt_default" or "LastChangedAt_manual".

Now the computed column shows the default column value if the column is not manually null, in which case it is displayed. In the StoragePattern model, the default column should be "Calculated."

This is an ugly solution, but it should work.

0
Jun 29 2018-10-10T00:
source share

I found it quite simple to create a partial class for Entity models similar to what you do for Data Annotations. Then I override the default constructor and set the default properties in the constructor. It works like a charm.

public partial class CallHistoryLog { public CallHistoryLog() { this.NumberFromId = -1L; this.NumberFromName = "NO NAME"; this.NumberToId = -1L; this.NumberToName = "NO NAME"; this.RouteId = -1L; this.Viewed = false; this.Deleted = false; this.DateCreated = DateTime.Now; } }

0
Apr 28 '16 at 19:56
source share

Hmm ... if you use EF6, it's actually a lot easier than you think. Just open your model, right-click on the column for which you want to set the default value, select the properties, and you will see the "DefaultValue" field. Just fill it in and save. He will set up a code for you.

The problem with some other solutions is that although they may work initially, as soon as you rebuild the model, it will throw out any custom code that you insert into the machine file.

So, under the hood, the user interface works by adding an additional property to the edmx file:

 <EntityType Name="Thingy"> <Property Name="Iteration" Type="Int32" Nullable="false" **DefaultValue="1"** /> 

And adding the necessary code to the constructor:

 public Thingy() { this.Iteration = 1; 
0
Mar 08 '17 at 19:45
source share



All Articles