实体框架 - 默认值并不在SQL Server表中设置 [英] Entity Framework - default values doesn't set in sql server table

查看:167
本文介绍了实体框架 - 默认值并不在SQL Server表中设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL Server 2005数据库表中有一栏createdon提供了缺省值设置为GETDATE()。我试图使用实体框架来添加一条记录。 createdon列未得到更新。

SQL server 2005 database table has a column 'createdon' for which default value set to getdate(). I am trying to add a record using entity framework. 'createdon' column is not getting updated.

我错过了在实体框架的任何财产,请建议。

Did I miss any property in Entity framework, please suggest.

推荐答案

这是是与实体框架问题的几个问题之一。假设你有一个类,它看起来像这样:

This is one of the few issues that are problematic with Entity Framework. Say you have a class that looks like this:

public class MyEntity
{
    // Id is a PK on the table with Auto-Increment
    public int Id { get; set; }

    // CreatedOn is a datetime, with a default value
    public DateTime CreatedOn { get; set; }
}

现在,你要插入一个新的元素:

Now, you want to insert a new element:

using(var context = new YourContext()) 
{
    context.MyEntities.Add(new MyEntity())
}

实体框架知道如何处理,因为在定义的自动递增的主键在EDMX。它不会尝试插入的编号属性的值。但是,只要实体框架而言, CreatedOn 的值:默认的DateTime 。由于实体框架不能说好,它有一个价值,但我应该忽略它,积极插入与 CreatedOn 属性值的记录,绕过的默认值的在你的表列的定义。

Entity Framework knows how to handle an auto-increment primary key because of the definition in the EDMX. It will not try to insert a value for the Id property. However, as far as Entity Framework is concerned, CreatedOn has a value: the default DateTime. Because Entity Framework cannot say "well, it has a value but I should ignore it", it will actively insert the record with the CreatedOn property value, bypassing the default value on your column definition on your table.

有没有简单的方法来做到这一点。您可以在插入该项目无论是积极的 CreatedOn 属性设置为 DateTime.Now 。或者你可以创建一个接口和一个扩展方法对:

There is no easy way to do this. You can either actively set the CreatedOn property to DateTime.Now when you insert that item. Or you can create an interface and an extension method pair:

public interface ICreatedOn
{
    public DateTime CreatedOn { get; set; }
}

public partial class MyEntity : ICreatedOn
{

}

public static TEntity AsNew<TEntity>(this TEntity entity) where TEntity : ICreatedOn
{
    if(entity != null)
        entity.CreatedOn = DateTime.Now;

    return entity;
}

using(var context = new YourContext()) 
{
    context.MyEntities.Add(new MyEntity().AsNew())
}   

修改:要扩大这一点,之所以这是一个无法解决的问题,是因为背后的自动增量字段的含义,并用默认值约束的领域。自动递增字段应该按照定义,总是由服务器处理,采用了种子和所有的爵士乐。您不能在插入的除非的你已经使用 SET IDENTITY INSERT ON 指定自动递增字段的值。默认值,然而,就是这样说:如果我不指定任何值,使用的提示。因为在.NET值类型不能为空,总是会有一个价值和Entity Framework无法推断的默认的该字段的值,在那个时候,意味着你希望它在默认SQL服务器。

To expand on this point, the reason why this is an unresolvable issue is because of the meaning behind an autoincrement field and a field with a default value constraint. An auto-increment field should, by definition, always be handle by the server, using a seed and all that jazz. You cannot specify a value for an auto-increment field on an insert unless you have used SET IDENTITY INSERT ON. A default value, however, is just a hint that say "if I don't specify any value, use this". Because value types in .NET cannot be null, there will always be a value and Entity Framework cannot infer that the default value for that field, at that time, means that you want it to be defaulted on the SQL server.

这篇关于实体框架 - 默认值并不在SQL Server表中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆