Issue with INSERT operation usine Breeze.Persistence.EFCore

59 views Asked by At

I am encountering a persistent issue with the Breeze.Persistence.EFCore v7.0.1 library while trying to save new entities. The error message I receive is:

Cannot insert the value NULL into column 'id', table 'dbo.Clients'; column does not allow nulls. INSERT fails.

Here's an overview of the situation:

  • The issue occurs when saving new entities.
  • I'm using a Guid data type for the Id property/column.
  • The Id value is automatically generated by breeze.js on the client side.
  • This Id value is successfully transmitted to the server and is present in the entity.
  • The Id property is explicitly marked with the [Key] attribute.
  • UPDATE and DELETE are working correctly.

I have verified the entity's state on the server, and it shows the Id with a valid value. This suggests that the issue might not be with the entity's Id assignment or transmission. I suspect there might be a configuration or compatibility issue with Breeze.Persistence.EFCore, especially considering the other operations work fine.

Debugged it till HandleAdded() method where there is a // HACK comment. May it be related somehow? IsTemporary property is set to true there but the RealValue stays null.

Entity details before save

Has anyone faced a similar issue or have insights on what could be causing this specific INSERT operation to fail? Any suggestions or guidance would be greatly appreciated.

Thank you in advance for your assistance!

P.S. it's not the first project I develop using Breeze so I believe I made most things right (as usual) so I believe I've set up everything correctly.

UPD

  • Updated to v7.2.1 latest stable - didn't help
  • According to @steve-schmitt advise I tried to set Id manually before save but that didn't help. BeforeEntitySave() methods fires first and just rewrites the client's value and then IsTemporary is set to true is doesn't care of the value origin and fails the same wasy

UPD2 (workable)

  • As @gert-arnold mentioned I forgot to set AutoGeneratedKeyType to None. Fixed that on client side (breeze.js):
import * as breeze from 'breeze-client';

let manager = new breeze.EntityManager(this.serviceUrl);
manager.fetchMetadata().then(() => {
                register(this.manager.metadataStore)
            });

function extend(store: breeze.MetadataStore) {
    metadataStore.getAsEntityType("Client")
        .setProperties({ autoGeneratedKeyType: breeze.AutoGeneratedKeyType.None });
}

After this new entities do not have id at all and set on the server side via BeforeEntitySave(). Despite this is not the way I expected still it works.

Going to open an issue on GitHub and then update the post.

1

There are 1 answers

2
Steve Schmitt On

The problem is that AutoGeneratedKeyType is Identity. You can see that in your QuickWatch screen shot.

Identity means that the database will generate the keys, so Breeze should not attempt to insert anything into that column. That's why it makes the RealValue NULL.

I don't think there is a Breeze AutoGeneratedKeyType of "Guid", but that's what would be needed here. Instead, you'll need to use AutoGeneratedKeyType.None and manually generate your Guids on the client.