我在插入数据库时​​遇到问题 [英] I have problem with inserting in data base

查看:81
本文介绍了我在插入数据库时​​遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.当我想首先将一个对象插入Db时,我收到此错误,我的主键是另一列,然后我创建此列并设置为主键.

错误:

Hi. i get this error when i want insert an object to Db at first my primary key was another column and then i create this column and set to primary key.

error:

Cannot insert the value NULL into column 'Id', table 'HomeWorkSystem.dbo.CourseGroup'; column does not allow nulls. INSERT fails.
The statement has been terminated.





[Column(Storage="_Id", AutoSync=AutoSync.OnInsert, IsPrimaryKey=true, IsDbGenerated=true)]
public int Id
{
    get
    {
        return this._Id;
    }
    set
    {
        if ((this._Id != value))
        {
            this.OnIdChanging(value);
            this.SendPropertyChanging();
            this._Id = value;
            this.SendPropertyChanged("Id");
            this.OnIdChanged();
        }
    }
}

推荐答案

如果您将_id列作为PrimaryKey而不是Identity,那么现在您无需在DB中插入任何值.

1-修改表格,使_id列标识为



2-为属性属性列"设置IsDbGenerated = false
If you have _id column as PrimaryKey, but not Identity, now you do not insert any value to DB.

1 - Modify your table, make _id column identity

or

2 - set IsDbGenerated=false for your property attribute "Column"


这篇关于我在插入数据库时​​遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆