Entity Framework Code First 创建数据库
Entity Framework Code First Create Database
我是 Entity Framework 的新手。 我想把它做成数据库作为项目中的一个单独的实体。 所以我从 Class 库项目开始了我的项目。 从 Login 开始,因此 class 通过 inherit IdentityUser 为它创建。
public class ApplicationUser : IdentityUser
{
[Required]
[MaxLength(200)]
public string FirstName { get; set; }
[Required]
[MaxLength(500)]
public string LastName { get; set; }
}
现在我想创建数据库,我在 App.Config.
中添加了密钥public class AppDBContext : DbContext
{
public AppDBContext():base("AppDBConnectionString")
{
}
}
第一次创建数据库时有包管理器命令吗? 更新数据库没有工作。
好的,总而言之,更新您的 class 以包含一个密钥:
public class ApplicationUser : IdentityUser
{
[Key]
public int ApplicationUserId { get; set; }
[Required]
[MaxLength(200)]
public string FirstName { get; set; }
[Required]
[MaxLength(500)]
public string LastName { get; set; }
}
并将 AppDbContext 中的 DbSet 更新为:
DbSet<ApplicationUser> ApplicationUsers { get; set; }