I have entity with foreign keys Project
:
public class Project : IEntity
{
public int Id { get; set; }
public int? TypeId { get; set; }
public virtual Type? Type { get; set; } = null!;
// other properties...
}
I want to create a new object (insert a new entity into the database):
await Context.Set<TEntity>().AddAsync(entity);
And I have the following problem:
Microsoft.Data.SqlClient.SqlException (0x80131904): It is not possible to insert an explicit value for the identifiers column in the "Types" table when the IDENTITY_INSERT parameter is set to OFF.
So, when trying to execute this SQL query, I have an exception:
SET NOCOUNT ON;
INSERT INTO [Types] ([Id], [Name])
VALUES (@p0, @p1);
There's no need to create a new Type
in the Types
table. How can I fix this? I understand that I can set IDENTITY_INSERT to OFF, but I think this isn't a good practice.