插入时的Entity Framework数据库默认设置允许更新 [英] Entity Framework database defaults on inserts allow updates

查看:33
本文介绍了插入时的Entity Framework数据库默认设置允许更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以让实体框架在插入内容上使用SQL默认值,而又允许更新该字段.我们有一个实例,其中一个SQL表具有一个标识列"id"和另一个设置为ident_current("table")的列.我知道要使用默认值插入字段的唯一方法是将字段设置为 DatabaseGenerated(DatabaseGeneratedOption.Computed),以便在插入时将其忽略.但是,通过具有该属性,我们将无法对列进行更新.而且它是一个自引用外键,因此我们无法进行插入然后立即更新来解决此问题.不要问我为什么表格是这样设计的-就像以前那样设置,所以我们现在还停留在它上面.下面是我们设置的简单示意图:

Is there a way to have entity framework use a SQL default value on an insert and yet allow updating to the field. We have an instance where a SQL table has an identity column "id" and another column which is set to ident_current("table"). The only way that I know of to get the field inserted with the default value is to set the field as DatabaseGenerated(DatabaseGeneratedOption.Computed) so that it is ignored on the insert. However by having that attribute then we cannot perform an update to the column. Also it's a self referencing foreign key so we can't do an insert then immediate update to get around the issue. Don't ask me why the table is designed this way - just the way it was set up before so we're kind of stuck with it for now. A simple diagram of our setup is below:

DomainClass:

DomainClass:

Class1 {
     public int id {get;set;}
     [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
     public int id2 {get;set;}
     public string Name {get;set;}
}

SQL(伪):

Table (
    id INT  which is an identity(1,1) column,
    id2 INT NOT NULL with a default value of ident_current("table")
    Name nvarchar(50)
)

我们希望EF生成的insert语句为:

We would want the insert statement generated by EF to be:

INSERT INTO Table(Name) VALUES('Name') 

,更新为:

UPDATE table 
SET id2 = *somenumber*, name = 'Name'

非常感谢您提供的所有帮助.如果需要的话,我们也使用EF 4.3.1.0.

Thanks a lot for all the help. We are using EF 4.3.1.0 if that's needed as well.

推荐答案

无法使用AFAIK.参见

There is no way AFAIK. See this and that.

第一个链接指向有关使用序列作为主键的建议,这似乎是您可能想要做的,而不是给出示例代码.

The first link points to a suggestion about using sequences as primary keys, which seems like something you might want to do instead given your example code.

第二个链接指向有关默认值的一般处理的建议,当前也不支持该建议,但是这可能是增加对所需内容的支持的另一个潜在起点.

The second link points to a suggestion about generic handling of default values, which is currently not supported either, but would be another potential starting point toward adding support for what you need.

这篇关于插入时的Entity Framework数据库默认设置允许更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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