Trying to insert a row into my database using Entity Framework. I assign all of the values upon insert except ID, which is the primary key, and I'd like it to get auto-incremented by the SQL Server DB.
My table definition/class:
public partial class T_SurfacePick
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public int? BoreholeID { get; set; }
public int? AuthoredNameID { get; set; }
public int? LoadFormat { get; set; }
public double? PickDepth { get; set; }
public int? OwningAuthorID { get; set; }
public int? QualityID { get; set; }
public double? CustomData { get; set; }
}
When I call the SaveChanges()
method, I get an error
Cannot insert the value NULL into column 'ID'
How do I get this column to auto-increment itself? I'd like not to have to worry about keeping track of the ID
and assigning it myself.
I'm trying to use the code-first approach. Here's an example of part of my decision tree for inserts/updates:
if (MenefeePick == null) // New top
{
var Pick = new T_SurfacePick { BoreholeID = Wellbore.ID, AuthoredNameID = MenefeeAN.ID, LoadFormat = 1, PickDepth = Menefee, OwningAuthorID = ProgAuthor.ID, QualityID = -1, CustomData = null };
db.T_SurfacePick.Add(Pick);
}
else // Update top
{
MenefeePick.PickDepth = Menefee;
}
db.SaveChanges();
I'll further qualify by mentioning that my model uses an existing database from a software product.