Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF. (Entity Framework Core)

asp.net-core c# entity-framework-core

Popular Answer

For upcoming readers.

The opposite issue arose for me. I did not want IDENTITY to turn on automatically.

So let's say this: (very simple fluent mapping)

        builder.HasKey(ent => ent.MyColumnWhichIsTheKey);

To keep IDENITY off (intentionally "off"), I had to add the following line (ValueGeneratedNever):

        builder.HasKey(ent => ent.MyColumnWhichIsTheKey);
        builder.Property(ent => ent.MyColumnWhichIsTheKey).ValueGeneratedNever();

See:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.builders.propertybuilder.valuegeneratednever?view=efcore-3.1

Other cues

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

and

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.3" />
0
4/14/2020 2:35:11 PM


Related Questions





Related

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