Conditionally set Identity field when pasting with Entity Framework

I am using the ADO.NET Firebird provider with the Entity Framework, but this question also applies to other providers.

I have a field on my model as follows

[Column("JOBNO"), DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int JobNo { get; set; } 

In the database, I have a β€œon insert” trigger that updates the JOBNO field with a generator if JOBNO set to NULL

By setting the DatabaseGenerated attribute in DatabaseGeneratedOption.Identity in the model field, the framework structure correctly pulls JOBNO out of the database upon insertion.

However, sometimes I want to manually specify the JOBNO column when inserting, but EF does not understand and simply uses the generated value.

Is there a way to enable this conditional setting of the DatabaseGenerated field?

+6
source share
1 answer

I am afraid that there is no use of a conditional setting for the DataBaseGenerated field !

1st option: Whether the behavior of the insert trigger should be transferred to your application (instead of the database), this way you will always generate an identifier from the application.

The second option: DO NOT specify an identifier and leave NULLABLE in the database. This method will allow EF to receive data, and when it is not received, you get the value from the trigger. (But please be sure to change the trigger so that it simply creates a new identifier when it has not been provided!)

0
source

Source: https://habr.com/ru/post/953891/


All Articles