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

查看:19
本文介绍了实体框架 - 默认值未在 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.

推荐答案

这是 Entity Framework 有问题的少数问题之一.假设您有一个如下所示的类:

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 中的定义,实体框架知道如何处理自动递增的主键.它不会尝试为 Id 属性插入值.然而,就实体框架而言,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())
}   

为了扩展这一点,这是一个无法解决的问题的原因是因为 autoincrement 字段和带有 的字段背后的含义默认 值约束.根据定义,自动增量字段应始终由服务器处理,使用种子和所有爵士乐.除非您使用了 SET IDENTITY INSERT ON,否则您不能为插入的自动增量字段指定值.然而,默认值只是一个提示,表示如果我没有指定任何值,请使用它".因为 .NET 中的值类型不能为空,所以总会有一个值并且实体框架无法推断该字段的 default 值,当时,意味着您希望它在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天全站免登陆