I am using Linq for SQL. I have a DataContext that I am against .SubmitChanges () 'ing. Error entering identifier field:
You cannot insert an explicit value for the identity column in the Rigs table when IDENTITY_INSERT is set to OFF.
The only identifier field is "ID", which has a value of 0. It is defined in DBML as:
[Column(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
There are several foreign keys, and I checked that they have values ββthat are jive with the contents of the external tables.
Why should I get this error?
Edit: Here is the request:
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
It is clear that it passes zero, even though it has 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(); }
In addition, I notice that when I UPDATE an existing installation (insertonsubmit), it does not update. The profiler does not even launch any queries.