[英]Adding values to database with an auto-increment Id
我想知道如何為輸入的每個體重輸入一個不同的ID。
控制台應用程序第一次運行良好,但沒有在表中添加任何行。
我的控制台代碼:
class Program
{
int weightInput = 0;
int calorieInput = 0;
private void addWeight()
{
Weight weight = new Weight();
weight.date = DateTime.Today;
weight.kg = weightInput;
using (weighttrackerEntities context = new weighttrackerEntities())
{
context.Weights.Add(weight);
context.SaveChanges();
}
}
}
我的Weight
實體:
public partial class Weight
{
public int Id { get; set; }
public int kg { get; set; }
public System.DateTime date { get; set; }
}
您應該使用模型構建器將ID標記為自動遞增
modelBuilder.Entity<Foo>()
.Property(f => f.Id)
.ValueGeneratedOnAdd();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.