开发者

Set default value in EF designer datetime

Trying to set the default value of a datetime field to take current time. In SQL's field designer i'd use get开发者_如何学Pythondate(). What should i use in Entity Framework's designer?

thx


Set 'StoredGeneratedPattern' to Computed against the field in the EDMX.

You still need the default value in SQL Server though, the above setting will ensure EF honour's that.


Building upon RPM1984's answer:

  1. Select the field that has the default value,Created_On in my example, within your EDMX file
  2. Go to the Properties panel
  3. Select the StoreGeneratedPattern attribute
  4. Then change the value to Computed

Set default value in EF designer datetime


kidos for this answer although it didn't worked for me after setting StoreGeneratedPattern attribute value to Computed but setting StoreGeneratedPattern attribute value to Identity worked for me , i was setting default "Guid" to UserID of UNIQUEIDENTIFIER type


Setting default values in Entity Framework 5 and 6 by changing T4 Template File

Made below changes in .tt(template file) remove if condition at line 34

34 if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
35 {

add

59 OnCreated();
60 }
61
62 partial void OnCreated();
63 <#

refer this image http://i.stack.imgur.com/DdlNB.png red means remove and green means add

This will add constructor in all entity classes with OnCreated method.

Like below

public partial class Category
{
    public Category()
    {
      this.Products = new HashSet<Product>();
      OnCreated();
    }

 partial void OnCreated();
 public int Id { get; set; }
 public string Name { get; set; }

 public virtual ICollection<Product> Products { get; set; }
}

Then create class file using same namespace that of Entities.

public partial class Category
{
   partial void OnCreated()
   {
      Name = "abc"
   }
}

refer below answer for more details https://stackoverflow.com/a/38882032/5475124


Right click edmx, open with, choose xml editor, find "ProviderManifestToken" change from 2008 to 2005. Save.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜