❔ I'm having issue with this Insert method with linq
I'm trying to insert into my sql database, but i get the error "Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF" even tough i'm executing identity_insert on/off.
18 Replies
what ORM is that?
doesn't look like EF
also, what database are you using?
i'm using the .net framework and SQL
".net framework" isn't an ORM
are you using Dapper? EF? raw ado.net?
and what do you mean by sql? microsoft sql server?
I see a
dc
here, but I don't know what dc
isI'm using microsoft sql server
DataClasses1DataContext dc = new DataClasses1DataContext(); this is the dc
i'm not sure about ORM i'm kinda new to this
ahhhh, this is linq-to-sql any of you familiar with linq-to-sql?
yeah, and i'm hating every bit of it
It's EF Core's distant cousin whose family tree is a family circle
yeah ik
but I have no idea how to use it
@Infinity 2789 you may want to move to #database
sure, thanks
Well, the error means, that you have some auto-generated primary key
And you're trying to insert something of your own there
that's what the
set identity_insert
is there forMaybe it needs to be executed first, or explicitly wrapped in a transaction or something
the problem is even if i leave the identity_insert on, its like i never turned it on
i get the error anyway
At any time, only one table in a session can have the IDENTITY_INSERT property set to ON. If a table already has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another table, SQL Server returns an error message that states SET IDENTITY_INSERT is already ON and reports the table it is set ON for.global state 🤢 what the hell sql server?
but i'm not sure what i'm writing inside the identity column, cause i'm trying to autogenerate an id, so i'm just filling the other data and no that
so i should try to insert each data 1 at the time?
Well, apparently you're trying to insert into the identity column as well
Also, Jesus, I just noticed that
.Wait()
I'm guessing, since there doesn't seem to be any DTOs there, you're passing a whole, uh, Allievi
s around
Perhaps, at some point, the ID gets set...?i'll try to debug and check it out
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.