How to use Default column value from DataBase in Entity Framework?
How to use Default column value from DataBase in Entity Framework?
I have a Date column in table which has default value or binding as getutcdate(). I want to use this in entity framework.On generating EDM I was able to find "Default Value " property at column level but I think it is for hardcoded value.
Please let me know how can I use default value specified in database.
Answer by mmtache for How to use Default column value from DataBase in Entity Framework?
StoreGeneratedPattern = "Computed" is not the same as a default value, because this property would be updated EVERY TIME with the default value in the database.. meaning it is impossible to update it manually.
Answer by mvr for How to use Default column value from DataBase in Entity Framework?
You can set the 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 the XML mapping is generated, your change will be lost.
Another way to do it is to set the value yourself in your code to DateTime.UtcNow. You could set this in your entity's constructor (define a new constructor if necessary), or you could set it in your own event handler for your context's SavingChanges event (see How to: Execute Business Logic When Saving Changes (Entity Framework) for an example of handling the SavingChanges event).
Answer by Asaf R for How to use Default column value from DataBase in Entity Framework?
Here's a possible, but not pretty workaround -
Setting Computed on a column will make it read only, but will make the default value work. It's possible to have a real Computed column, say "LastChangedAt_computed" that either shows the value of "LastChangedAt_default" or "LastChangedAt_manual".
Now, the computed column shows the value of the default column, unless the manual column is not-null, in which case it is shown. In the model, the StoragePattern of the default column needs to be "Computed".
It's an ugly solution, but it should work.
Answer by Heather for How to use Default column value from DataBase in Entity Framework?
Implementing the OnCreated event for the entity is the solution I have found. I had a Guid property that I wanted to be populated. By default it was being populated with all zeros (00000-0000-00000-etc). By adding the following to my partial class of the entity I was able to deal with the issue.
partial void OnCreated() { Guid = Guid.NewGuid(); }
Answer by crokusek for How to use Default column value from DataBase in Entity Framework?
A problem with setting StoreGeneratedPattern = "Computed" or "Identity" is that they do not allow the client to ever provide a value. Running into this issue on inserts but also for updates.
Seems like another option or two is needed for StoreGeneratedPattern so the database can at least see the user provided values but override it if need be. This would allow any existing DB insert or update triggers that update one field based on another field to work. For instance a DB trigger on an update might update the modified timestamp only if one is not provided and only if certain fields were updated.
Perhaps the column extended attributes feature in SQL Server could be used to set that field automatically during extraction so we don't end up editing XML files.
Answer by NER1808 for How to use Default column value from DataBase in Entity Framework?
There is another solution suggested in this post by using a partial class and a method in the constructor to set the values.
How to use the default Entity Framework and default date values
Answer by user6268526 for How to use Default column value from DataBase in Entity Framework?
what I found that is pretty simple is to create a partial class for the Entity model(s) similar to what you do for Data Annotations. Then I override the default constructor and set the default properties in the constructor. 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; } }
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment