简体   繁体   English

使用Entity Framework插入时有条件地设置Identity字段

[英]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.

相关问题 使用实体框架插入时设置 Id 字段 - Set Id field when inserting using entity framework 实体框架6.1.1-当StoreGenerated为Identity时,以编程方式设置ID - Entity Framework 6.1.1 - Programmatically set ID when StoreGenerated is Identity 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表'[table]'中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF 当 IDENTITY_INSERT 设置为 OFF 时,无法为标识列插入显式值。 (实体框架核心) - Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF. (Entity Framework Core) 使用实体框架在代码优先迁移中有条件地插入数据 - Conditionally inserting data in a code-first migration with Entity Framework 实体框架4.2,无法设置标识插入ON - Entity Framework 4.2, Unable to set Identity Insert ON 实体框架代码优先不会将身份设置为是 - Entity Framework Code First will not set Identity to Yes 实体框架:手动设置“身份”值 - Entity framework : Manually set an “Identity” value 使用Entity Framework和动态Json将范围标识插入第二个表 - Inserting Scope identity into second table using Entity Framework and dynamic Json 实体框架插入记录,其中组合键未插入第二个字段 - Entity Framework inserting record with composite key not inserting second field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM