Where do I place the DropCreateDatabase code in an ASP.net MVC project to have the database created anew?
public class DropCreateDatabaseAlways<TContext> : System.Data.Entity.IDatabaseInitializer<TContext> where TContext : DbContext
Where do I place the DropCreateDatabase code in an ASP.net MVC project to have the database created anew?
public class DropCreateDatabaseAlways<TContext> : System.Data.Entity.IDatabaseInitializer<TContext> where TContext : DbContext
IFirst attempt:
Step 1
lplace createdatabasealways in data context.
Error:
Missing Assembly Reference or Using Directive
Step 2
tried adding
// using System.Data.Linq;
// using System.Data;
// using System.Data.Object;
Error Remains:
Missing Assembly Reference or Using Directive
What exactly are you trying to do? Execute pending migrations when the application starts?
Browser: I have a working Azure website with links in the navigation bar that navigate to the appropriate views.
Microsoft SQL Server Management Studio: property set (database) and properties (columns) appear in database on Azure, however the properties (columns) include older versions (form all the code-first migrations from the beginning of the project to present) and there is no data - no records.
My notes including screen captures in a pdf
What I think I want to do is update-database as it exists today over the existing database on Azure along with the data / records that are in my localDB.
I think that you do this with some variation of createdatabase (on model change or always), so I'm trying to set that up.
Please correct me if I'm wrong. Any help appreciated.
Hi @DeanAndrewEverhart-9294,
Maybe you can create a class to implement DropCreateDatabaseAlways and call Database.SetInitializer function in Application_Start().
-DbInitializer
public class MyInitializer : DropCreateDatabaseAlways<YourContextName>
{
protected override void Seed(MagnateContext context)
{
// seed database here
}
}
-Application_Start
protected void Application_Start()
{
Database.SetInitializer(new MyDbInitializer());
}
The database will be create when running the application.
And if you would like to do a automatic migration of database, use MigrateDatabaseToLatestVersion class.
public class Configuration : DbMigrationsConfiguration<YourContextName>
{
public Configuration()
{
this.AutomaticMigrationsEnabled = true;
}
}
-Application_Start
Database.SetInitializer(new MigrateDatabaseToLatestVersion<YourContextName,Configuration>());
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Lan Huang
3 people are following this question.