I've got a problem, while trying to insert a new row into a SQL Server database with help of LINQ to SQL.
Here is the code:
[HttpPost]
public ActionResult QuestionsAnswers(UserQuestion userQuestion)
{
if (ModelState.IsValid)
{
var newUserQuestion = new QuestionAnswer();
newUserQuestion.Question = userQuestion.Question;
newUserQuestion.From = userQuestion.From;
ViewBag.QuestionSent = true;
QuestionAnswerDb.QuestionAnswers.InsertOnSubmit(newUserQuestion);
QuestionAnswerDb.SubmitChanges();
return View(AnswersList);
}
ViewBag.QuestionSent = false;
ViewBag.From = userQuestion.From;
ViewBag.Question = userQuestion.Question;
return View(AnswersList);
}
And I get an error while trying to "submit" a new UserQuestion
(that has only From
and Question
in it), QuestionAnswer has columns: Id, From, Question, Answer
(this one is nullable):
Cannot insert explicit value for identity column in table 'QuestionAnswer' when IDENTITY_INSERT is set to OFF.
I thought that Linq-to-SQL should handle Identity autoincrement columns by itself. Now I think, that I was wrong...
Any clues on how to properly set an Id for this object?
Thanks in advance.