Forum Discussion

apenaranda's avatar
apenaranda
Copper Contributor
Sep 08, 2022

Create database connectionStrings ASP.NET

Summarizing a little what I have, the app creates a sql database, if it is already created, it deletes it and recreates it.
I would like to understand the part of the code where it does this, which if I'm not mistaken is this.

 

 

 

 

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
            Database.SetInitializer(new IdentityDropCreateInitializer());
        }

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

 

 

 

 

 

Could you help me understand this part of asp.net code?
In the end, what I would like to do is tell the program not to create the database, just insert tables and data, but the database neither creates nor deletes it.
Thank you.

5 Replies

  • Nathan_Hobbs's avatar
    Nathan_Hobbs
    Copper Contributor

    Hi, apenaranda thank you for sharing such amazing info. this code is work perfectly but I have one question how do we insert the delete the database again with the same name or the same file in SQL? 

  • LanHuang's avatar
    LanHuang
    Former Employee

    Hi apenaranda,

    Thanks for posting your issue here.

    However this platform is used for how-to discussions and sharing best practices for building any app with .NET.Since your issue is a technical question, welcome to post it in Microsoft Q&A forum, the support team and communities on Microsoft Q&A will help you for any technical questions.
    Besides, it will be appreciated if you can share it here once you post this technical question Microsoft Q&A.
    Best Regards,
    Lan Huang

    • apenaranda's avatar
      apenaranda
      Copper Contributor
      Thank you very much, I will do so and as soon as I have a resolution I will share it.
      Cheers

Resources