Set Entity Framework connection string at runtime

后端 未结 2 1896
终归单人心
终归单人心 2021-01-15 23:28

I have generated entity model from AdventureWorks database; now I want to delete the connection string in app.config and set it at runtime. In the Model1.Context.cs file I h

2条回答
  •  忘掉有多难
    2021-01-15 23:33

    I'd go with something like:

     public AdventureWorksEntities(string server, string databaseName, string user, string password)
            :base(new System.Data.EntityClient.EntityConnectionStringBuilder
            {
                Metadata = "res://*",
                Provider = "System.Data.SqlClient",
                ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder 
                {
                    InitialCatalog = databaseName,
                    DataSource = server,
                    IntegratedSecurity = false,
                    UserID = user,
                    Password = password,
    
                }.ConnectionString
            }.ConnectionString) 
        {
    
        }
    

提交回复
热议问题