Create database connectionStrings ASP.NET

Albert Peñaranda 1 Reputation point
2022-09-08T09:00:59.683+00:00

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.

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-09-08T19:59:31.643+00:00
    0 comments No comments

  2. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2022-09-09T06:07:43.243+00:00

    Hi @Albert Peñaranda ,
    You can learn more about the use of the Database.SetInitializer method in C# in the following documentation:

    https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.database.setinitializer?view=entity-framework-6.2.0

    To add, delete, and modify the database in the ASP.NET, we first need to bind the database under the Web.Config file ASP.NET.

    <connectionStrings>  
               <add name="TEST" connectionString="xxxxxxxxx;" providerName="System.Data.SqlClient"/>  
    </connectionStrings>  
    
    string strcon = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;// Reads the connection string from the web.config file  
                SqlConnection con = new SqlConnection(strcon);// Defines the connection object  
                SqlCommand cmd = new SqlCommand();//Create a command object  
               cmd.Connection = con;// Sets the database connection properties of the command object  
                cmd.CommandText = "xxxxx";// Assign SQL statements to the command object  
                try  
                {  
                    con.Open();//Open a database connection  
                    SqlDataReader sdr = cmd.ExecuteReader();//Execute sql commands and get the query results  
                    cmd.Connection = con;// Sets the database connection properties of the command object  
                    cmd.CommandText = "select * from student";// Assign SQL statements to the command object  
                    con.Close();//Close the database connection  
                    con.Open();//Open a database connection  
                    sdr = cmd.ExecuteReader();  
              }  
    catch (Exception ex)  
                {  
                    Response.Write(ex.Message);  
                }  
                finally  
                {  
                    if (con.State == System.Data.ConnectionState.Open) // Determine whether the database connection is closed  
                        con.Close();//Close the database connection  
                }  
    

    The addition, deletion, and modification of the operation is based on your SQL command. If you want to implement more features, please give me more details and I will do my best to help you solve the problem.

    Best regards,
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.