Linq以sql错误插入值

时间:2013-11-21 14:36:11

标签: linq-to-sql

我正在尝试使用linq到sql进行插入,但是我收到以下错误 附加信息:无法将值NULL插入列'UserID',表'Itiss_Request.dbo.Users';列不允许空值。 INSERT失败。

UserID表是pk aswel,因为标识已设置为autoincrement。 该数据库有4个字段。

        DataClasses1DataContext dt = new DataClasses1DataContext();
        User usr = new User();
        usr.MudID = a[1];
        usr.Email = Session["email"].ToString();
        usr.Name = Session["userName"].ToString();


        dt.Users.InsertOnSubmit(usr);

        dt.SubmitChanges();

这是我的上下文文件

[global :: System.Data.Linq.Mapping.ColumnAttribute(Storage =“_ UserID”,AutoSync = AutoSync.OnInsert,DbType =“Int NOT NULL IDENTITY”,IsPrimaryKey = true,IsDbGenerated = true)]         public int UserID         {             得到             {                 return this._UserID;             }             组             {                 if((this._UserID!= value))                 {                     this.OnUserIDChanging(值);                     this.SendPropertyChanging();                     this._UserID = value;                     this.SendPropertyChanged( “用户ID”);                     this.OnUserIDChanged();                 }             }         }

1 个答案:

答案 0 :(得分:0)

请试试这个......

    DataClasses1DataContext dt = new DataClasses1DataContext();
    User usr = new User();
    usr.MudID = a[1];
    usr.Email = Session["email"].ToString();
    usr.Name = Session["userName"].ToString();
    dt.Users.AddObject(usr);
    dt.SaveChanges();