EF Code-first Learning Journey Database Initialization

Source: Internet
Author: User

The base class constructor for the context class has the following arguments

1. No parameters

If you do not add a parameter to the base class constructor, it creates the database in the local SQLEXPRESS server named {Namespace}. {Context class name}

namespace schooldatalayer{    publicclass  context:dbcontext     {        public  Base()        {                    }     }}

The database name created above is:schooldatalayer.context

2. The parameter is: database name

If the base class constructor specifies the name of the database, the local SQLEXPRESS database server is created with the name given to you.

namespace schooldatalayer{    publicclass  context:dbcontext     {        public  base("myschooldb")         {                           }}    }

The database name above is:myschooldb

3. The parameter is: connection string name

You can create a connection string in app. Config or Web. config, and code first creates a database based on the connection string if the parameters used by the base class constructor have been name=

 namespace   schooldatalayer{ public  class   Context:dbcontext { public  schooldbcontext (): base  ("  name= schooldbconnectionstring   " ) {}} }
<?xml version="1.0" encoding="utf-8" ?>< configuration>    <connectionStrings>    <add name="schooldbconnectionstring "      connectionString="Data source=.;i Nitial catalog=schooldb-byconnectionstring;integrated security=true"     providerName ="System.Data.SqlClient"/>    </connectionstrings></ Configuration>

EF Code-first Learning Journey Database initialization

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.