VS introduces the powerful functionality of the Entity Framework, making it possible to quickly build a DB based application. For the Entity Framework This article does not do too much description, is actually Microsoft version of the O-r Mapping. This article only talks about how to dynamically modify the information of a database connection string.
VS provides a wizard to generate the Entity Framework to access the database's connection string and store the connection string in the Web.config file, which we typically generate at development time. When we release the product to the customer it is clear that the connection string's information needs to be modified, which needs to be changed dynamically through the code.
We can do this through local classes and local methods, with the following specific code:
Public partial class Developentities {partial void oncontextcreated () {SqlConnectionStringBuilder sb = new SqlConnection StringBuilder (((entityconnection) Connection). storeconnection.connectionstring); Sb. IntegratedSecurity = false; Sb. UserID = "User1"; Sb. Password = "xxxxxxx"; ((entityconnection) Connection). storeconnection.connectionstring = sb. ConnectionString; } }
Just a little code on it, very simple, hehe