给定像
这样的实体class Foo
{
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public int SometimesComputed { get; set; }
}
在插入时,生成的SQL将返回SometimesComputed
的值:
INSERT
...
RETURNING "SometimesComputed" INTO "SometimesComputed"
OPEN :p0 FOR SELECT "SometimesComputed" AS "SometimesComputed" FROM DUAL;
哪个好。但是,DatabaseGeneratedOption.Computed
或DatabaseGeneratedOption.Identity
的警告是,我在插入前为SometimesComputed
指定的任何值都将被忽略,并替换为DB计算值。
var foo = new Foo { SometimesComputed = 42 };
context.DbSet<Foo>().Add(foo);
context.SaveChanges();
// foo.SometimesComputed will be whatever the next sequence value was, not 42
是否可以让实体框架执行RETURNING ... INTO OPEN FOR SELECT
无论如何?无论我指定一个值,是否由触发器设置,只需返回插入的值。