The column cannot contain null values. SqlCE exception

末鹿安然 提交于 2019-12-23 05:29:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!