Web. config database connection string
<connectionStrings>
<add name= "LocalSqlServer" connectionstring= "Data source=.\sqlexpress;integrated Security=sspi; attachdbfilename=| Datadirectory|aspnetdb.mdf; User instance=true "providername=" System.Data.SqlClient "/>
</connectionStrings>
<connectionStrings> Property Description
1. Data Source
The "Data Source" can be replaced by the following strings: "Server", "Address", "Addr", and "Network address".
2. Integrated Security
"Integrated Security" can be written as "Trusted_Connection".
True to use the current Windows account credentials for authentication, false when the user ID and password need to be specified in the connection. The recognized values are true, False, Yes, No, and SSPI that is equivalent to true.
If you do not have these, you must write the settings such as uid=sa;pwd=123, "UID" can also use "User ID", "pwd" can also be changed to "PassWord".
3, AttachDbFileName
"AttachDbFileName" can be written as "Extended Properties", "Initial file name".
The AttachDbFileName property specifies the location of the database file that is dynamically attached to the server when the connection is opened. Use | datadirectory| syntax, this path is replaced by the application's App_Data directory at run time.
4.Initial Catalog
Equivalent to Database.
Common connection Strings
<add name= "Market" connectionstring= "Data source= (local), Integrated security=true,initial Catelog=market" Providername= "System.Data.SqlClient"/>
Global.asax
1 protected void Application_Start () 2 {3 // Build Database 4 Database.setinitializer (new dropcreatedatabaseifmodelchanges<marketmodels.marketmodelscontext>()); 5 }
View Code
Modelcontext
1 Public classMarketmodelscontext:dbcontext2 {3 PublicMarketmodelscontext ()4:Base("Name=market")5 {}6 Public VirtualDbset<category> Categories {Get;Set; }7}View Code
Initializer
1 classMarketmodelsinitializer:dropcreatedatabaseifmodelchanges<marketmodelscontext>2 {3 protected Override voidSeed (marketmodelscontext context)4 {5 varCategory =NewList<category>6 {7 //root directory8 NewCategory9 {TenCategoryid=NewGuid ("00000000-0000-0000-0000-000000000000"), OneName="root directory", ACode=999999, -Createtime=DateTime.Now, - } the }; -Category.foreach (c=>context. Categories.add (c)); - } -}View Code
MVC Basic Configuration Code