实体框架不创建数据库

时间:2018-05-20 01:53:14

标签: entity-framework

我正在尝试创建代码,首先创建数据库。我有2个连接字符串:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=DESKTOP-1111\SERVER_2014; database=the_userdb;user=sa; password=myPassword" providerName="System.Data.SqlClient" />
    <add name="PmisDatabaseContext" connectionString="Data Source=DESKTOP-1111\SERVER_2014; database=the_database;user=sa; password=myPassword" providerName="System.Data.SqlClient" />
</connectionStrings>

起初,我可以使用此功能添加迁移:

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool IsActivated { get; set; }
    public DateTime EnrollDate { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //AspNetUsers -> User
        modelBuilder.Entity<ApplicationUser>()
            .ToTable("User");
        //AspNetRoles -> Role
        modelBuilder.Entity<IdentityRole>()
            .ToTable("Role");
        //AspNetUserRoles -> UserRole
        modelBuilder.Entity<IdentityUserRole>()
            .ToTable("UserRole");
        //AspNetUserClaims -> UserClaim
        modelBuilder.Entity<IdentityUserClaim>()
            .ToTable("UserClaim");
        //AspNetUserLogins -> UserLogin
        modelBuilder.Entity<IdentityUserLogin>()    
            .ToTable("UserLogin");
    }
}

然后我'启用了迁移'然后'添加迁移InitialDatabase'然后'update-database'并且能够成功创建一个包含登录表的新数据库。

现在我想用一个名为ImageLibraries的表创建一个新数据库,所以我在我的模型中这样做了:

[Table("ImageLibrary")]
public class ImageLibrary
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int Id { get; set; }
    [Required]
    public string UserId { get; set; }
}

然后补充说:

public class PmisDatabaseContext : DbContext
{
    public PmisDatabaseContext()
        : base("name=PmisDatabaseContext")
    {
        Database.SetInitializer<PmisDatabaseContext>(new CreateDatabaseIfNotExists<PmisDatabaseContext>());
    }


    public virtual DbSet<ImageLibrary> ImageLibraries { get; set; }
}

当我尝试'添加迁移AddedImageLibrary'时,在我的迁移文件夹中,我得到'up()'和'down()',但没有任何内容。它不是创建新的数据库和表。你能告诉我如何做到这一点。谢谢。

0 个答案:

没有答案