EF Attempting to Insert into Identity Field of Custom Entity

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 năm cách đây
Hi,

I have created a custom entity, following the basic steps as outlined to me in various posts, through adding the class to Nop.Core.Domain, adding the entity map to Nop.Data.Mapping, adding a new service, and finally developing the UI.

Where I am currently stuck at is INSERTING a record.  I am getting the follwing error:

Cannot insert explicit value for identity column in table 'ReorderReminder' when IDENTITY_INSERT is set to OFF


I know this error is due to an attempt to insert an explicit value into the Id field on the table, which is defined as INT IDENTITY(1,1) NOT NULL.

I cannot work out how to address this in the code.  As far as I can see my custom entity is the same as the core ones.  It inherits BaseEntity.  The map identifies the Id as the key:


public ReorderReminderMap()
        {
            this.ToTable("ReorderReminder");
            this.HasKey(reminder => reminder.Id);


I must be missing a step somewhere, but I can't work out where.

Can anyone help..??
10 năm cách đây
TDSnet wrote:
Hi,

I have created a custom entity, following the basic steps as outlined to me in various posts, through adding the class to Nop.Core.Domain, adding the entity map to Nop.Data.Mapping, adding a new service, and finally developing the UI.

Where I am currently stuck at is INSERTING a record.  I am getting the follwing error:

Cannot insert explicit value for identity column in table 'ReorderReminder' when IDENTITY_INSERT is set to OFF


I know this error is due to an attempt to insert an explicit value into the Id field on the table, which is defined as INT IDENTITY(1,1) NOT NULL.

I cannot work out how to address this in the code.  As far as I can see my custom entity is the same as the core ones.  It inherits BaseEntity.  The map identifies the Id as the key:


public ReorderReminderMap()
        {
            this.ToTable("ReorderReminder");
            this.HasKey(reminder => reminder.Id);


I must be missing a step somewhere, but I can't work out where.

Can anyone help..??


Did you accidentally assigned a value to ReorderReminder when doing the Insert?
10 năm cách đây
It says: " when IDENTITY_INSERT is set to OFF". Have you tried setting it off?:
SET IDENTITY_INSERT tablename ON
10 năm cách đây
carlosmartinezt wrote:
It says: " when IDENTITY_INSERT is set to OFF". Have you tried setting it off?:
SET IDENTITY_INSERT tablename ON


Don't think this is the correct way to do. If you set IDENTITY_INSERT to ON, it means the value will not be auto-generated (or auto-incremented).
10 năm cách đây
Thanks guys, but perhaps I didn't explain clearly.  I don't WANT to insert an explicit value in the identity column, I want SQL to assign it as per standard behaviour.  My issue was that EF was generating a value for that column in it's insert statement.

Regardless, the issue turned out to be a by-product of my lack of knowledge of the code first approach to EF.  I had a couple of issues in my entity and mapping classes.  The way I identified the issues was by reverse engineering the classes from the database using the EF Power Tools plugin, and comparing them to what I had in my Nop solution.

All fixed..!!
4 năm cách đây
carlosmartinezt wrote:
It says: " when IDENTITY_INSERT is set to OFF". Have you tried setting it off?:
SET IDENTITY_INSERT tablename ON


It's amasing stupid answer
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.