我對如何正確設置模型感到困惑。下面你會看到我的POCO,我想知道如何自動增加ID的值,以及是否需要設置數據註釋[Key]。是否有某種使EF識別ID /主鍵的命名約定,還是必須使用[Key]數據註釋?如何正確設置模型,POCO類?
還有必要在子實體中設置[Key]數據註釋嗎?
public class User
{
[Key]
public int UserId { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public DateTime Reg { get; set; }
public virtual ICollection<Stats> Stats { get; set; }
}
public class Stats
{
[Key]
public int StatId { get; set; }
public string Age { get; set; }
public string Height { get; set; }
public string Weight { get; set; }
public bool Sex { get; set; }
}
public class BodylogContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Stats> Stats { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<BodylogContext>(null);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
謝謝我發現這真的很有幫助,感謝你的回答。 – brk