无法将值NULL插入列"ID" [英] Cannot insert the value NULL into column 'ID'

查看:119
本文介绍了无法将值NULL插入列"ID"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用Linq to Sql时,出现异常无法将值NULL插入列'ID'中",我将ID属性留为空白,但是我将ID设置为在DBML文件中自动生成.当我插入没有ID的代码并且不使用linq来让sql自动为我自动生成时,此方法会正常工作.问题是LINQ抛出异常是因为它看到ID为空,即使LINQ只是允许它通过SQL也可以.

I am getting the exception "Cannot insert the value NULL into column 'ID'" when using Linq to Sql , I am leaving the ID property blank , but I have set my ID to auto-generate in my DBML file. This will work fine when I insert without an ID and let sql auto generate for me not using linq. The problem is LINQ is throwing an exception because it sees that ID is empty , even though if LINQ would just let it go through SQL is ok with it.

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL", 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();
            }
        }
    }

推荐答案

您需要在列定义中包括"IDENTITY":

You need to include 'IDENTITY' in your column definition:

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int ID
{ ... }

如果无法解决,请尝试同时添加UpdateCheck=UpdateCheck.Never.

If that wont fix it try adding UpdateCheck=UpdateCheck.Never as well.

这篇关于无法将值NULL插入列"ID"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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