4
votes

My DBML Designer Code is this

[Column(Storage="_id",IsPrimaryKey=true,DbType="Int")]
public System.Nullable<int> id
{
    get
    {
        return this._id;
    }
    set
    {
        if ((this._id != value))
        {
            this._id = value;
        }
    }
}

and My C # Code this

DataClassesDataContext db = new DataClassesDataContext();
userlogin tc = new userlogin();

tc.username = TextBox1.Text;
tc.pass = TextBox2.Text;
db.userlogins.InsertOnSubmit(tc);
db.SubmitChanges();
Response.Redirect("Main.aspx");

when i am inserting data in text box error come Cannot insert explicit value for identity column in table 'userlogins' when IDENTITY_INSERT is set to OFF

1
The following are all orthogonal concepts. Just because they're frequently encountered together does not mean that any of them implies any of the others: a) an id column, b) a primary key, c) an identity column.Damien_The_Unbeliever

1 Answers

9
votes

Most likely your id field is autogenerated in DB, while it is not mark as such in code. Add the following property to the Column attribute:

IsDbGenerated=true

Or, if you are using visual editor for you LINQ model, mark this property as Auto Generated.