question

DeanAndrewEverhart-9294 avatar image
0 Votes"
DeanAndrewEverhart-9294 asked LanHuang-MSFT answered

DropCreateDatabase Code Location in ASP.net MVC project

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

Re: https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.dropcreatedatabasealways-1?view=entity-framework-6.2.0


dotnet-aspnet-core-mvcdotnet-aspnet-mvc
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


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

0 Votes 0 ·

What exactly are you trying to do? Execute pending migrations when the application starts?


0 Votes 0 ·
DeanAndrewEverhart-9294 avatar image
0 Votes"
DeanAndrewEverhart-9294 answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.