Entity Framework insert using Z.EntityFramework's SingleInsertAsync method returning IDENTITY

662 views Asked by At

I want to insert a single record into my database using the Z Entity Framework Extensions and the SingleInsertAsync method. However, I need the ID of the freshly inserted entity. How can I get that? I can literally find no documentation for this method.

Here's my code:

Specification newSpecification = db.Specifications.Create();

newSpecification.ID_Feature = featureID;
newSpecification.Specification1 = specificationKey;

await db.Specifications.SingleInsertAsync(newSpecification);

int id = newSpecification.ID;

The variable id is obviously 0 after the insert. How can I get the actual ID there? Unfortunately, SingleInsertAsync doesn't return anything.

1

There are 1 answers

2
Jonathan Magnan On

The SingleInsert works exactly like the BulkInsert method, so you can use the same documentation.

The method doesn't return the identity but should populate automatically the identity value.

If the Specification have the ID specified as identity, the value will be automatically populated.

See the following online example: https://dotnetfiddle.net/h9Psm7