我对如何正确设置模型感到困惑。下面你会看到我的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