❔ 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
jcotton42
jcotton422y ago
what ORM is that? doesn't look like EF also, what database are you using?
infinityluul
infinityluulOP2y ago
i'm using the .net framework and SQL
jcotton42
jcotton422y ago
".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 is
infinityluul
infinityluulOP2y ago
I'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
jcotton42
jcotton422y ago
ahhhh, this is linq-to-sql harold any of you familiar with linq-to-sql?
infinityluul
infinityluulOP2y ago
yeah, and i'm hating every bit of it
Angius
Angius2y ago
It's EF Core's distant cousin whose family tree is a family circle
jcotton42
jcotton422y ago
yeah ik but I have no idea how to use it @Infinity 2789 you may want to move to #database
infinityluul
infinityluulOP2y ago
sure, thanks
Angius
Angius2y ago
Well, the error means, that you have some auto-generated primary key And you're trying to insert something of your own there
jcotton42
jcotton422y ago
that's what the set identity_insert is there for
Angius
Angius2y ago
Maybe it needs to be executed first, or explicitly wrapped in a transaction or something
infinityluul
infinityluulOP2y ago
the problem is even if i leave the identity_insert on, its like i never turned it on i get the error anyway
jcotton42
jcotton422y ago
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?
infinityluul
infinityluulOP2y ago
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?
Angius
Angius2y ago
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, Allievis around Perhaps, at some point, the ID gets set...?
infinityluul
infinityluulOP2y ago
i'll try to debug and check it out
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server