EF 6 calls INSERT instead of UPDATE

.net c# entity-framework entity-framework-6 sql-server


It's possible that this is a duplicate query, but my extensive searches for the terms in the headline turned up many irrelevant results.

I have an entity that's roughly set up like this:

public abstract class A
    public GUID AId { get; set; }

    public string SomeProperty { get; set; }

public class B : A
    public string SomeOtherProperty { get; set; }

The setting haspublic DbSet<B> BInstances { get; set; } for B objects. InOnModelCreating since the mappingA and set to ignoredB linked to a table referred to asTableB .

The AId Although field is not an identification field and is not produced automatically, it is set as the primary key in the database and the mapping. The field is designated as a non-null in the database.uniqueidentifier without a default.

I'm loading an instance of during runtime ofB using its crucial_token is only aCancellationToken ):

var b = await (dbCtx.BInstances.FirstOrDefaultAsync(e => e.AId), _token));

Afterward, a feature ofb I attempt to save it back to the database once it is set:

b.SomeOtherProperty = "some new text";
await (dbCtx.SaveChangesAsync(_token));

Currently, the database is returning an error code of PRIMARY KEY constraint broken indicating that the value ofAId since it would be a duplication, it cannot be added. Naturally, because the ID was already present in the database, I imported the object using that information. I don't know why, but EF creates an INSERT statement rather than a UPDATE.

In my checkingdbCtx.Entry(b).State it's already configured toEntityState.Modified . Can someone explain to me what I'm doing incorrectly? I'm at a lost. Until recently, I never had any difficulties while changing entities, although I haven't utilised EF with GUID primary keys (I oftenlong principal keys).

I'm using.NET Framework 4.7.1 and EF 6.

6/26/2018 4:02:04 AM

Accepted Answer

Thank you for your comments; it turned out that the issue was a mapping issue that I introduced.

In myOnModelCreating() I made a call.MapInheritedProperties() on a type other than a base class that it didn't inherit fromobject , of course) — it seems that this set off an issue. The mapping call functioned correctly with other things that do share a base class.

Also, I nicknamedToTable() directly against the entity class — for reasons I do not understand, this ruined my table mapping. I then transferred that call inside.Map() It began operating as anticipated.

I continued from here:


on this:

entity.Map(m => m.ToTable("tablename"));

in order to fix the issue.

Hopefully, this will be helpful to readers in the future.

7/12/2018 4:17:03 PM

Popular Answer

Try it.

b.SomeOtherProperty = "some new text";
await (dbCtx.SaveChangesAsync(_token));

Your b instance will be updated by AddorUpdate if it has already been added.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow