MVC Basic Configuration Code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.