问题
I'm getting the following exception when I attempt to insert some sample data into a SQL Server CE 3.5 database inside a WP7 application:
Error: SqlCeException "The column cannot contain null values.
[ Column name = Name,Table name = Exam ]
The name
is also the primary key. I think I have declared everything properly in the DataContext
.
private void InsertData()
{
mangoDb = new ExamDataContext();
// create a new exam instance
Exam exam = new Exam();
exam.Name = "History";
exam.Description ="History Exam";
// add the new exam to the context
mangoDb.Exams.InsertOnSubmit(exam);
// save changes to the database
mangoDb.SubmitChanges();
}
Where have I gone wrong?
回答1:
I solved a problem like this modifying id
field declaration by adding IDENTITY (1,1)
so my id declaration becomes:
[id] int NOT NULL IDENTITY (1,1)
回答2:
I solved the problem by deleting the index(es) of The Table. Not sure why it worked, but it certainly did, in case anyone else comes across this issue.
来源:https://stackoverflow.com/questions/12511549/the-column-cannot-contain-null-values-sqlce-exception