【发布时间】:2013-04-13 19:56:53
【问题描述】:
我对如何正确设置我的模型有点困惑。下面你会看到我的 POCO,我想知道如何自动增加 ID,以及是否有必要设置数据注释 [Key]。是否有某种命名约定可以使 EF 识别 ID/主键,还是我必须使用 [Key] 数据注释?
还有必要在子实体中设置[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>();
}
}
【问题讨论】:
标签: c# asp.net-mvc entity-framework data-annotations poco