C
C#3y ago
Herbix

IDENTITY_INSERT Error on table in which I am not even trying to insert?

I am getting this Error: SqlException: Cannot insert explicit value for identity column in table 'Unterwerke' when IDENTITY_INSERT is set to OFF. ..when I am Trying to insert something into the table "Clients". Every Client has 1 Unterwerk but i am never inserting an Unterwerk in my code. This is how I insert the client:
public void Insert(Client entity)
{
_context.Clients.Add(entity);
_context.SaveChanges();
}
public void Insert(Client entity)
{
_context.Clients.Add(entity);
_context.SaveChanges();
}
3 Replies
Pobiega
Pobiega3y ago
You'll need to provide more context and code. Show Client and Unterwerke declarations and any EF Configurations
Grault
Grault3y ago
Every Client has 1 Unterwerk
That implies that inserting a Client also causes an Unterwerk to be inserted, unless some Clients do not actually have Unterwerks. But this problem is deeper than inserting an Unterwerk unexpectedly. Presumably you would want to eventually insert one, and at that point you will run into this problem anyway.
Herbix
HerbixOP3y ago
I've fixed it. @Grault you are correct, it wanted to insert a new Unterwerk, i just added UnterwerkID to the Model and filled that instead of the Unterwerk obejct
Want results from more Discord servers?
Add your server