0

当我尝试将一些示例数据插入 WP7 应用程序内的 SQL Server CE 3.5 数据库时,出现以下异常:

错误:SqlCeException“列不能包含空值。
[列名=名称,表名=考试]

name也是主键。我想我已经在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();
         }

我哪里出错了?

4

3 回答 3

1

我通过添加这样的修改id字段声明解决了这样的问题,IDENTITY (1,1)因此我的 id 声明变为:

[id] int NOT NULL  IDENTITY (1,1)
于 2012-12-19T05:39:33.283 回答
1

在我当前使用 .NetCore、SqlCE DB 和 Dapper ORM 的项目中,我和我的同事遇到了同样的问题。我们的 Id 列不是表中的标识列。甚至无法更改列

几个小时后,我们发现,表示主键列的模型类属性必须使用以下注释 [Key]、[Required] 进行修饰。

前任:

public class Exam
{
    [Key]
    [Required]
    public string Name { get; set; }

    // Other properties...
}
于 2020-12-17T16:45:37.537 回答
0

我通过删除表格的索引解决了这个问题。不知道为什么它会起作用,但它肯定会起作用,以防其他人遇到这个问题。

于 2012-09-21T01:29:10.693 回答