I can't insert data into my database because it says that the ID can't be null and I'm inserting the ID. Also it does not work when I don't specify the ID and leave the autonumeration in DB settings.
This is my code:
using Dapper;
using Dapper.Contrib.Extensions;
using System.Data;
using System.Data.SqlClient;
string ConnectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Students;Integrated Security=True";
InsertSingleStudent();
void InsertSingleStudent()
{
using (IDbConnection db = new SqlConnection(ConnectionString))
{
Student student = new Student()
{
FirstName = "William",
LastName = "Shakespeare",
Semester = 1,
Id = 1,
BirthDate = new DateTime(2023, 1, 1),
University="UBB",
Year = new DateTime(2022,1,1)
};
db.Insert<Student>(student);
}
}
The exact error is:
System.Data.SqlClient.SqlException: 'Cannot insert the value NULL into column 'Id', table 'Students.dbo.Students'; column does not allow nulls. INSERT fails.