Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot insert explicit value for identity column in table 'userlogins' when IDENTITY_INSERT is set to OFF?

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

like image 391
Ashok Bhanwal Avatar asked Jan 13 '23 02:01

Ashok Bhanwal


1 Answers

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.

like image 121
Andrei Avatar answered Jan 15 '23 16:01

Andrei