Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Feb 06, 2013 12:38 AM by Decker Dong - MSFT
Feb 04, 2013 08:12 PM|LINK
My app uses EF. Following code is simple, and in another context, was working. Now, when it goes to update the context, the "m" object's primary key (TSID) is set to zero, and, since there's a zero out there, it fails. TSID is an identity column. I don't
understand what I've done wrong... any ideas?
protected void btnCreateTimesheet_Click(object sender, EventArgs e)
using (CPASEntities cx = new CPASEntities())
tblTimesheetMaster m = new tblTimesheetMaster();
m.TSVendorID = Convert.ToInt32(ddlVendorID.SelectedValue);
m.TSApprover = ddlApproverName.SelectedItem.ToString();
m.TSOverrideApprover = ddlOverrideApproverName.SelectedValue.ToString();
m.TSStartDate = Convert.ToDateTime(tbWeekStart.Text);
m.TSAuthor = User.Identity.Name;
m.TSStatus = "New";
//CreateSheetMessage.Text = "Timesheet Added";
Response.Redirect("TimesheetEntry.aspx?id=" + m.TSID);
catch (Exception err)
CreateSheetMessage.Text = "Unable to Add a New Timesheet -- " + err.InnerException.Message;
CreateSheetMessage.Visible = true;
It catches the exception and says the Primary Key (TSID) has a value of zero, which is already in the table....
Feb 04, 2013 08:36 PM|LINK
Open your EF model. Then open properties of problematic class primary key property and see if it is marked as Identity field.
Feb 04, 2013 08:55 PM|LINK
It was not set (in the "Store Generated Pattern") to "Identity". I found that location by checking the ID of another table to compare. I set it to "Identity", saved and rebuilt everything, but it failed with the same message:
Unable to Add a New Timesheet -- Violation of PRIMARY KEY constraint 'PK_tblTimesheetMaster'. Cannot insert duplicate key in object 'dbo.tblTimesheetMaster'. The duplicate key value is (0). The statement has been terminated.
Feb 04, 2013 10:18 PM|LINK
Check from database if primary key field of this table is identity field or not (it's common mistake to forget to mark the field as identity field).
If this is not the problem and you are using some newer version of EF that uses .tt files then right click on your EF model file and select Run Custom Tool. Then all automatically generated classes are created again and they reflect all changes in model.
Feb 05, 2013 05:12 PM|LINK
You were exactly right. I had messed up the SQL Server stuff. I went and fixed that, but the model still did not properly update, so I deleted the tblTimesheetMaster entity and re-added it in the wizard. That worked. Now, I get another problem I don't understand.
The Timesheet Master is added fine now, but I get a thread exception (what's that?) when I try to redirect to the next page. I sent you an email with an image of the whole screen (it's big....) I'd post it here, but I'm baffled how to do
I get a null reference exception, but that's useless. Looks like perhaps the inner exception is:
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
A first chance exception of type 'System.NullReferenceException' occurred in Timesheet.DLL
Feb 05, 2013 08:05 PM|LINK
This error exists only in logs or is user able to see it too?
Feb 05, 2013 08:36 PM|LINK
I would guess only in log. User error was essentially that the "err" object was null. I can't figure out why it entered the catch block with a null err object.... I killed the try..catch block and everything worked fine.
I also solved my earlier problem by rewriting the markup. The "ddlVendorID" control was, indeed, missing. I regenerated the control and the datasource and the where param, and it all worked. Doubtless it was my fault, but I did get it working.
Feb 06, 2013 12:38 AM|LINK
I come here for the last checking.
If your problem is solved, I'll mark answers to close your issue.
Otherwises please feel free to feedback.