-
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
EF trying to Insert into Identity field #6426
Comments
@Dethorhyne This is a bug in how we handle the data annotation. You can work around this by using Fluent API in modelBuilder
.Entity<ApplicationUser>()
.Property(u => u.NumericId)
.UseSqlServerIdentityColumn(); |
…Strategy for SqlServer Fixes #6426
…Strategy for SqlServer Fixes #6426
…Strategy for SqlServer Fixes #6426
…Strategy for SqlServer Fixes #6426
@brentdavid2008 Can you please open a new issue including a complete code listing or project that reproduces what you are seeing. |
+1. |
Use Metadata property like this : Before Entity Frmework core 2.0 :
for Entity framework core 2.0 , The "IsReadOnlyAfterSave" property is deprecated.Use following :
|
@mkiani3000 I tried to do exactly as you explained on EF Core 2.0 and I'm getting this error message when running
Here's the code in the var scenarioBuilder = modelBuilder.Entity<Person>();
scenarioBuilder.Property(p => p.Id).UseSqlServerIdentityColumn();
scenarioBuilder.Property(p => p.Id).Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore; |
@Gimly Keys must be marked as PropertySaveBehavior.Throw--key values cannot be changed after the entity has been saved. This is the default, so nothing should need to be set. |
I had this issue too!! I tried to add an annotation [Key, Column(Order = 1)] for my property, but it doesn't work. Try the method below. I use it to fix my problem (.Net Core and EF Core 1.1.1)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Adding the code below tells DB "NumericId is an AlternateKey and don't update".
modelBuilder.Entity<User>()
.HasAlternateKey(x => x.NumericId ).HasName("IX_NumericId");
}
Ref: |
Hello @ajcvickers I m getting this type of error while Inserting a new data to the database, my database is postgresql {"message":"An error occurred while updating the entries. See the inner exception for details.","detail":" at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(DbContext _, ValueTuple |
@Vaishnavi6121995 Please create a new issue and include a small, runnable project/solution or complete code listing that demonstrates the exception you are seeing. |
I'm guessing this is now
|
Steps to reproduce
The issue
For some reason EF is attempting to insert value into an Identity column which isn't allowed and throws an exception. Rest of the columns do NOT get updated.
However,
this bug also accured when I tried to register, but in that case, database wise, everything was Inserted, new user was registered and a proper NumericId was assigned to the user even though the front-end threw the exception. Few builds after, even though nothing has changed, registering stopped throwing the exception and functions properly.
Further technical details
EF Core version: 1.0.0
Operating system: Windows 7 Professional
Visual Studio version: VS 2015 Enterprise edition
Other details about my project setup:
None.
The text was updated successfully, but these errors were encountered: