-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot insert explicit value for identity column in table 'Table2' when IDENTITY_INSERT is set to OFF #14968
Comments
I’m also facing the same issue. What is work around solution sine the fix is tag to version 3.0.0? |
@baoduy The workaround is to explicitly specify the property as not generated. For example: modelBuilder.Entity<Table2>().Property(a => a.Table1Id).ValueGeneratedNever(); |
Thanks @ajcvickers , I suspected this issue also happened with owned type |
Stilll have the same issue even with |
@rymix I just ran the code above but with the following: modelBuilder.Entity<Table2>().Property(e => e.Table1Id).ValueGeneratedNever();
modelBuilder.Entity<Table2>().Property(e => e.Key).ValueGeneratedNever(); It worked for me, so it looks like you may be hitting something different. Can you file a new issue and include a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing. |
We discussed in triage whether to retain this change in behavior--that is, that if there is a composite key where only one of the properties is numeric and not a foreign key, then it should be made store-generated and hence use an Identity column on SQL Server. While this does make sense (although the key space is larger than it needs to be) it would be a breaking change (as reported here) and on balance the value of doing this is not worth the break. We will keep the new behavior for owned collections. |
I have this issue on 3.1.5. Can you please advise the solution? |
This work-around doesn't work to me. ` [Table ("OrderDetails")] DBContext.cs: builder.Entity() builder.Entity() Error message: Failed executing DbCommand (24ms) [Parameters=[@p0='2', @p1='9268', @p2='0', @p3='2020-06-18T13:16:55' (Nullable = true), @P4='1120' (Nullable = true), @p5='False' (Nullable = true), @p6='False' (Nullable = true), @P7='2020-06-18T13:16:55' (Nullable = true), @p8='1120' (Nullable = true), @p9='http://res.cloudinary.com/dzvbuqogl/image/upload/v1576804505/qcojj1xyg2gr2iqvqrof.jpg' (Size = 4000), @p10='15.4', @p11='4086' (Nullable = true), @p12='1', @P13='Swi Calcuim 100tablets' (Size = 4000), @p14='15.4', @P15='0.35' (Nullable = true)], CommandType='Text', CommandTimeout='60'] |
EF adds identity on composite key where it shouldn't - On EF core 2.2 this Identity was not set when creating a composite key.
This is the exception I get when trying to add a new row:
Steps to reproduce
I'm adding a code that you will be able to run and reproduce it.
Pay attention for the Identity that was added to the Key column
Bellow is the schema that was generated:
Further technical details
EF Core version: 3.0.0-preview3.19153.1
Database Provider: (Microsoft.EntityFrameworkCore.SqlServer
Operating system:
IDE: Visual Studio 2017 15.9.7
The text was updated successfully, but these errors were encountered: