[英]Conditionally set Identity field when inserting with Entity Framework
I'm using the Firebird ADO.NET provider with Entity Framework, but this question would also apply to other providers. 我正在使用带有Entity Framework的Firebird ADO.NET提供程序,但这个问题也适用于其他提供程序。
I have an field on my model as follows 我的模型上有一个字段如下
[Column("JOBNO"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int JobNo { get; set; }
In the database I have an 'on insert' trigger which updates the JOBNO
field using a generator if JOBNO
is set to NULL
在数据库中,我有一个'on insert'触发器,如果
JOBNO
设置为NULL
则使用生成器更新JOBNO
字段
By setting the DatabaseGenerated
attribute to DatabaseGeneratedOption.Identity
on my model's field, entity framework correctly pulls the JOBNO
from the database on insert. 通过设置
DatabaseGenerated
属性DatabaseGeneratedOption.Identity
在我的模型的领域,实体框架正确地拉JOBNO
从上插入数据库。
However sometimes I want to manually specify the JOBNO
column when inserting, but EF doesn't understand and just uses the generated value. 但有时我想在插入时手动指定
JOBNO
列,但EF不理解并只使用生成的值。
Is there a way to allow this conditional setting of a DataBaseGenerated
field? 有没有办法允许
DataBaseGenerated
字段的这种条件设置?
I'm afraid there is no way to do a conditional setting for a DataBaseGenerated field ! 我担心没有办法为DataBaseGenerated字段进行条件设置 !
1st option: Is to have the behavior of the insert trigger transfered to your application (instead of the database), this way you will always generate the id from the application. 第一个选项:是否将insert trigger的行为转移到您的应用程序(而不是数据库),这样您将始终从应用程序生成id。
2nd option: Is NOT TO specify the Identity and leave as NULLABLE on the database.This way you will allow EF to receive the data and when it does not receive you get the value from the trigger. 第二个选项:不是指定标识并在数据库上保留为NULLABLE。这样您将允许EF接收数据,当它没有收到时,您将从触发器获取值。 (But please make sure to alter the trigger so it just create a new id when it was not provided!)
(但请确保更改触发器,以便在未提供时创建新的ID!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.