DropCreateDatabase Code Location in ASP.net MVC project

Dean Everhart 1,496 Reputation points
2021-09-08T10:13:57.99+00:00

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://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dropcreatedatabasealways-1?view=entity-framework-6.2.0

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,308 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,386 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Dean Everhart 1,496 Reputation points
    2021-09-08T19:11:46.537+00:00

    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.

    0 comments No comments

  2. Lan Huang-MSFT 28,126 Reputation points Microsoft Vendor
    2021-09-15T05:27:44.053+00:00

    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

    0 comments No comments