Skip to content
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 entity into DB using attach and EntityState.Added #8264

Closed
d0pare opened this issue Apr 23, 2017 · 1 comment
Closed

Cannot insert entity into DB using attach and EntityState.Added #8264

d0pare opened this issue Apr 23, 2017 · 1 comment
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@d0pare
Copy link

d0pare commented Apr 23, 2017

I am getting following exception when trying to insert entity by attaching and changing its state.

Microsoft.EntityFrameworkCore.DbUpdateException occurred
  HResult=0x80131500
  Message=An error occurred while updating the entries. See the inner exception for details.
  Source=Microsoft.EntityFrameworkCore.Relational
  StackTrace:
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(Tuple`2 parameters)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](Func`2 operation, Func`2 verifySucceeded, TState state)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at EFCoreTest.Program.Main() in C:\Users\Dopare\documents\visual studio 2017\Projects\EFCoreTest\EFCoreTest\Program.cs:line 22

Inner Exception 1:
SqlException: Cannot insert explicit value for identity column in table 'Persons' when IDENTITY_INSERT is set to OFF.

Steps to reproduce

Sample repo

using (var db = new AppDbContext())
{
    var person = new Person
    {
        FirstName = "FirstName",
        LastName = "LastName",
        BirthDate = DateTime.Now
    };

    db.Persons.Attach(person);
    db.Entry(person).State = EntityState.Added;

    db.SaveChanges();
}

Further technical details

EF Core version: 1.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 x64
IDE: (e.g. Visual Studio 2015)

@ajcvickers ajcvickers self-assigned this Apr 24, 2017
@ajcvickers ajcvickers added this to the 2.0.0 milestone Apr 24, 2017
@ajcvickers
Copy link
Member

@dopare Attaching the entity means that the entity already exists in the database. This means that no key generation is done--because the key must already have had a value assigned to be saved int the database. On the other hand, if Add is called, then it means that the entity is new and has not yet been saved to the database. In this case key generation will kick in, ultimately using the Identity column in the database

In this case, because the entity was first Attached, and then later changed to Added, it is not getting key generation. However, we are considering changing this so that even if the entity is first Attached, then later when it is made Added it will get the same key generation that would have happened if it was made Added in the first place.

ajcvickers added a commit that referenced this issue Jun 28, 2017
…state

Issue #8264, which was already fixed in 2.0 by the changes Attach/TrackGraph handling.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

2 participants