1

I have started a new project which is hosted by Azure - and I am trying to let it create the tables automatically.

I have a model:

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

And in my Global.asax:

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

And, finally the 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; }
}

And after running my project, it does not create the tables. Or am I doing something wrong which I'm not seeing?

1
  • Hello Kevin, is your DefaultConnection connection string correct? Commented May 19, 2016 at 14:53

2 Answers 2

1

@user3435421 Yes, I think that is the right approach. The only thing I can add is that the original poster will need to enable migrations using the enable-migrations command before he/she calls the Add-Migration command. You can find the process described in relation to the Contoso University example here: 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

Sign up to request clarification or add additional context in comments.

Comments

1

I believe you first have to update your database.

Try this in your Package Manager Console.

PM> Add-Migration UpdateName

It should create a new file with all changes in it. Do a careful check on the file to make sure it understands what you try to achieve. Then:

PM> Update-Database

This command executes the migration. The new table should have been created now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.