【问题标题】:EntityFramework 6 - Create tables?EntityFramework 6 - 创建表?
【发布时间】:2016-05-19 14:40:23
【问题描述】:

我已经启动了一个由 Azure 托管的新项目 - 我正在尝试让它自动创建表。

我有一个模型:

 public class Account
{
    [Key]
    [Required]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Surname { get; set; }
    [Required]
    [EmailAddress]
    public string Email { get; set; }
}

在我的 Global.asax 中:

Database.SetInitializer(new CreateDatabaseIfNotExists<ApplicationDbContext>());

最后是 ApplicationDbContext:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
        // Add custom user claims here
        return userIdentity;
    }
}

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

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<Account> Accounts { get; set; }
}

运行我的项目后,它不会创建表。还是我做错了什么我没有看到?

【问题讨论】:

  • 您好,Kevin,您的DefaultConnection 连接字符串正确吗?

标签: c# entity-framework visual-studio


【解决方案1】:

@user3435421 是的,我认为这是正确的方法。我唯一可以补充的是,原始发布者需要在调用 Add-Migration 命令之前使用 enable-migrations 命令启用迁移。您可以在此处找到与 Contoso 大学示例相关的过程:http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application

【讨论】:

    【解决方案2】:

    我相信您首先必须更新您的数据库。

    在您的包管理器控制台中试试这个。

    PM> Add-Migration UpdateName
    

    它应该创建一个包含所有更改的新文件。仔细检查文件以确保它理解您尝试实现的目标。那么:

    PM> Update-Database
    

    此命令执行迁移。现在应该已经创建了新表。

    【讨论】:

      猜你喜欢
      • 2014-03-12
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多