Closed SimonOrdo closed 7 years ago
In your code
entity.Property(e => e.Id) .HasColumnName("ID") .ValueGeneratedNever();
You have configured not to generate values for ID column in DiscountCodes_Uses table. When you add DiscountCodesUses
entity to context, EF will just send value as is to database (since there is no value generation configured). In your case, since you have not set the value of Id
it takes the default value of int which is 0. You need to provide a value for Id
property or set value generation for it.
Thanks, @smitpatel
Two things:
.ValueGeneratedNever()
line... and nowException is:
InnerException = {"Cannot insert the value NULL into column 'ID', table 'dbo.DiscountCodes_Uses'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}
@SimonOrdo - What is the definition in database for table DiscountCodes_Uses
@smitpatel
CREATE TABLE [dbo].[DiscountCodes_Uses](
[ID] [bigint] NOT NULL,
[DiscountCodeId] [bigint] NOT NULL,
[ConsumerId] [nvarchar](450) NOT NULL,
[ConsumedOn] [datetime] NOT NULL,
[OrderItemId] [bigint] NOT NULL,
CONSTRAINT [PK_DiscountCodes_Uses] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO
ALTER TABLE [dbo].[DiscountCodes_Uses] WITH CHECK ADD CONSTRAINT [FK_DiscountCodes_Uses_AspNetUsers] FOREIGN KEY([ConsumerId])
REFERENCES [dbo].[AspNetUsers] ([Id])
GO
ALTER TABLE [dbo].[DiscountCodes_Uses] CHECK CONSTRAINT [FK_DiscountCodes_Uses_AspNetUsers]
GO
ALTER TABLE [dbo].[DiscountCodes_Uses] WITH CHECK ADD CONSTRAINT [FK_DiscountCodes_Uses_DiscountCodes] FOREIGN KEY([DiscountCodeId])
REFERENCES [dbo].[DiscountCodes] ([ID])
GO
ALTER TABLE [dbo].[DiscountCodes_Uses] CHECK CONSTRAINT [FK_DiscountCodes_Uses_DiscountCodes]
GO
ALTER TABLE [dbo].[DiscountCodes_Uses] WITH CHECK ADD CONSTRAINT [FK_DiscountCodes_Uses_Payments_Orders] FOREIGN KEY([OrderItemId])
REFERENCES [dbo].[Payments_OrderItem] ([ID])
GO
ALTER TABLE [dbo].[DiscountCodes_Uses] CHECK CONSTRAINT [FK_DiscountCodes_Uses_Payments_Orders]
GO
Answer to 1:
Your ID
column does not have identity (or default value etc) set hence Reverse engineer added ValueGeneratedNever
to reflect what is in the database i.e. never generate values through any mechanism.
Answer to 2:
Since your database is not configured to generate any values, your database needs you to provide value of the column every time you are trying to insert something. When ValueGeneratedNever
call was made, EF concluded that we don't have to generate values hence we will send whatever value you provided while adding the entity which is 0
and we sent it. (hence first exception)
When you remove call to ValueGeneratedNever
in the runtime model, you made the property as ValueGenerated (by convention it would Identity). Since the value set is 0 which is default(long), we take it as no value set and the value should be generated by database. This sends Insert statement without including the column ID
. Hence the 2nd exception.
Given that your database is not configured to use any value generation, whenever you are adding new entity, you need to explicitly specify the value of Id
column.
Oh geez...!
I've spent hours looking at this!
Sorry for wasting your time and thanks for your help!
I'm create a few related entities and attempting to save them. The entities are new, but for some reason EF is attempting to insert the primary key column causing a duplicate primary key exception (attempting to insert an entity with Key '0', which already exists in the DB.
Offending code:
Order Item Entity:
Code use entity:
The error I get is `{"Violation of PRIMARY KEY constraint 'PK_DiscountCodes_Uses'. Cannot insert duplicate key in object 'dbo_DiscountCodes_Uses'. The duplicate key value is (0).\r\nThe statement has been terminated."}'
The generate SQL is as follows:
As you can see, EF attempts to erroneously
INSERT
the[ID]
column.As an aside, everything works fine if I don't attempt to add a
DiscountCodesUses
record. I.e. all the other tracking/id relationships seems to work.Is this an EF issue or am I doing it wrong?
Thanks in advance!