Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 156999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:25:32+00:00 2026-05-11T10:25:32+00:00

I’m using Linq to SQL. I have a DataContext against which I am .SubmitChanges()’ing.

  • 0

I’m using Linq to SQL. I have a DataContext against which I am .SubmitChanges()’ing. There is an error inserting the identity field:

Cannot insert explicit value for identity column in table ‘Rigs’ when IDENTITY_INSERT is set to OFF.

The only identity field is ‘ID’, which has a value of 0. It’s defined in the DBML as:

[Column(Storage='_ID', AutoSync=AutoSync.OnInsert, DbType='Int NOT NULL IDENTITY', IsPrimaryKey=true, IsDbGenerated=true)] 

There are a few foreign keys, and I’ve verified they have values that jive with the foreign tables’ content.

Why would I be getting this error?

Edit: Here is the query:

exec sp_executesql N'INSERT INTO [dbo].[Rigs]([id], [Name], [RAM], [Usage], [MoreInfo], [datetime], [UID]) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6) SELECT [t0].[id], [t0].[OSID], [t0].[Monitors] FROM [dbo].[Rigs] AS [t0] WHERE [t0].[id] = @p7',N'@p0 int,@p1 varchar(1),@p2 int,@p3 varchar(1),@p4 varchar(1),@p5 datetime,@p6 int,@p7  int',@p0=0,@p1='1',@p2=NULL,@p3='4',@p4='5',@p5=''2009-03-11 20:09:15:700'',@p6=1,@p7=0 

Clearly it is passing a zero, despite having never been assigned a value.

Edit: Adding Code:

Rig rig = new Rig(); int RigID; try { // Confirmed these always contain a nonzero value or blank     RigID = int.Parse(lbSystems.SelectedValue ?? hfRigID.Value);     if (RigID > 0) rig = mo.utils.RigUtils.GetByID(RigID); } catch { }  rig.Name = Server.HtmlEncode(txtName.Text); rig.OSID = int.Parse(ddlOS.SelectedValue); rig.Monitors = int.Parse(txtMonitors.Text); rig.Usage = Server.HtmlEncode(txtUsage.Text); rig.MoreInfo = Server.HtmlEncode(txtMoreInfo.Text); rig.RigsToVideoCards.Clear(); foreach (ListItem li in lbYourCards.Items) {     RigsToVideoCard r2vc = new RigsToVideoCard();     r2vc.VCID = int.Parse(li.Value);     rig.RigsToVideoCards.Add(r2vc); } rig.UID = c_UID > 0 ? c_UID : mo.utils.UserUtils.GetUserByToken(this.Master.LiveToken).ID;  if (!mo.utils.RigUtils.Save(rig))        throw new ApplicationException('There was an error saving your Rig. I have been notified.'); hfRigID.Value = rig.id.ToString();  public static User GetUserByToken(string token) {     DataClassesDataContext dc = new DataClassesDataContext(ConfigurationManager.ConnectionStrings['MultimonOnlineConnectionString'].ConnectionString); return (from u in dc.Users     where u.LiveToken == token     select u).FirstOrDefault(); } 

Also, I notice that when I UPDATE an existing rig (insertonsubmit), it doesn’t update. Profiler doesn’t even show any queries being run.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:25:33+00:00Added an answer on May 11, 2026 at 10:25 am

    Is your code setting the ID value explicitely to 0? (instead of leaving it untouched).

    Update 1: As you posted on the updated version, linq2sql is clearly passing the value to the db. Here is one I haven’t had any trouble with:

    [Column(Storage='_CustomerID', AutoSync=AutoSync.Always, DbType='Int NOT NULL IDENTITY', IsDbGenerated=true)] public int CustomerID  

    I just saw another one, and it has the same exact definition of the one you are using.

    [Column(Storage='_TestID', AutoSync=AutoSync.OnInsert, DbType='Int NOT NULL IDENTITY', IsPrimaryKey=true, IsDbGenerated=true)] public int TestID 

    Update 2: Regarding updates, you are not supposed to do InsertOnSubmit for that. Just update the values and call .SubmitChanges (should be throwing an exception). On the insert it is really weird, as the property attributes you posted seems to be correct, so the Insert method linq2sql generates should be correct as well. I would try, re-adding the table on the designer again and verifying all the properties are correct.

    Note that the generated insert method should look like (with a matchingRig_Insert):

    private void InsertRig(Rig obj) {     System.Nullable<int> p1 = obj.Id; 

    this.Rig_Insert(/* bunch of values */, ref p1); obj.Id = p1.GetValueOrDefault(); }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.