C# EntityFramework 不使用默认值更新列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18506088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EntityFramework not updating column with default value
提问by t_plusplus
I am inserting an object into a SQL Server db via the EntityFramework 4 (EF). On the receiving table there is a column of (CreatedDate
), which has its default value set to getdate()
. So I do not provide it to the EF assuming its value will be defaulted by SQL Server to getdate()
.
我正在通过 EntityFramework 4 (EF) 将一个对象插入到 SQL Server 数据库中。在接收表上有一列 ( CreatedDate
),其默认值设置为getdate()
。所以我不会将它提供给 EF,假设它的值将被 SQL Server 默认为getdate()
.
However this doesn't happen; instead EF return a validation error n SaveChanges()
.
然而,这不会发生;相反 EF 返回一个验证错误 n SaveChanges()
。
Is there any reason that you know for this happening? Please let me know.
您是否知道发生这种情况的任何原因?请告诉我。
Many thanks.
非常感谢。
采纳答案by mattmanser
If you never want to edit that value (like with a created date), you can use:
如果您永远不想编辑该值(例如使用创建日期),您可以使用:
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public virtual DateTime CreatedDate { get; set; }
This will tell the Entity Framework that the value is controlled by the database, but will still fetch the value.
这将告诉实体框架该值由数据库控制,但仍将获取该值。
Note that you then cannot change that value, so it's not a solution if you simply want an initial value.
请注意,您无法更改该值,因此如果您只是想要一个初始值,这不是解决方案。
If you just want a default value but are still allowed to edit it, or you are using the Entity Framework 5 and below, you have to set the default in code.
如果您只想要一个默认值但仍允许对其进行编辑,或者您使用的是实体框架 5 及以下版本,则必须在代码中设置默认值。
More discussion about this here:
更多关于这里的讨论:
How to use Default column value from DataBase in Entity Framework?
回答by kkahl
Just apply the [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
attribute to on the column field in your entity object definition.
只需将[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
属性应用于实体对象定义中的列字段。
For example:
例如:
public class SomeTable
{
...
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime CreatedDate { get; set; }
...
}
This tells the Entity Framework that your column's initial value is supplied by the database. The value will update automatically from the database after row insertion.
这告诉实体框架您的列的初始值由数据库提供。行插入后,该值将自动从数据库更新。
回答by Shaheen K
To get to work i declared a new variable and set that variable to the current time. Then when inserting the data i referenced the variable to the column:
为了开始工作,我声明了一个新变量并将该变量设置为当前时间。然后在插入数据时,我将变量引用到列:
var db = new YourTableEntities();
var dateNow = DateTime.Now;
db.YourTableEntities.Add(new YourTableEntities()
{
ColumnAInTable = someAVariable,
ColumnBInTable = someBVariable,
ColumnThatShouldDefaultToGetDate = dateNow
});
Hope this helps!
希望这可以帮助!
回答by CptRobby
The correct answer to this issue is to tell Entity Framework that the column is Computed.
这个问题的正确答案是告诉实体框架该列是计算的。
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public virtual DateTime CreatedDate { get; set; }
DatabaseGeneratedOption.Computed
means that it is set by the database and can not be changed by Entity Framework. DatabaseGeneratedOption.Identity
means that the column is an IDENTITYcolumn, which should only used for autoincrementing numeric primary keys.
DatabaseGeneratedOption.Computed
意味着它是由数据库设置的,不能被实体框架更改。DatabaseGeneratedOption.Identity
意味着该列是一个IDENTITY列,它应该只用于自动递增数字主键。
**Note that the documentation for the DatabaseGeneratedOption enumerationdoesn't mention anything about IDENTITY columns (partly because that is a SqlServer specific implimentation detail). Instead it defines the Identity option as "The database generates a value when a row is inserted." I was actually looking for a way to allow a ModDate to be set using a DEFAULT constraint when the record is created, but to allow it to be modified from Entity Framework when updating the record. So because of the description, I thought I might have found a solution, but trying to modify the ModDate of a record when it was flagged with DatabaseGeneratedOption.Identity
threw an exception. So no luck there.
**请注意,DatabaseGeneratedOption 枚举的文档没有提到有关 IDENTITY 列的任何内容(部分原因是这是 SqlServer 特定的实现细节)。相反,它将 Identity 选项定义为“数据库在插入一行时生成一个值”。我实际上是在寻找一种方法,允许在创建记录时使用 DEFAULT 约束设置 ModDate,但允许在更新记录时从实体框架修改它。所以因为描述,我想我可能已经找到了一个解决方案,但是当它被标记为时试图修改记录的 ModDateDatabaseGeneratedOption.Identity
抛出了异常。所以没有运气。
回答by CESAR NICOLINI RIVERO
try to use this.
尝试使用这个。
public virtual DateTime CreatedDate { get; set; } = new Datetime();