实体框架代码首先创建数据库

时间:2016-01-02 14:09:54

标签: c# asp.net-mvc entity-framework code-first

我是Entity Framework的新手。 我想将数据库作为一个单独的Entity In项目。 所以我用类库项目开始了我的项目。 从Login开始,使用继承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")
    {

    }
}

首次创建数据库的任何Package Manager命令? 更新数据库无法正常工作。

1 个答案:

答案 0 :(得分:1)

好的,总结一下,更新您的课程以包含一个键:

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; }