Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


C#
[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();
        }
    }
}
Posted
Comments
R. Giskard Reventlov 31-May-12 12:36pm    
So what's the question?
Rajesh Kariyavula 31-May-12 13:04pm    
Are you sure the ID column is Identity column in DB?
Rajesh Kariyavula 31-May-12 13:52pm    
Null will be passed when the DataContext is not able to generate the primary key value from the DataBase as it is an identity column, please recheck you table.

1 solution

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"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900