zoukankan      html  css  js  c++  java
  • Entity Framework Code-First(6):Database Initialization

    Database Initialization:

    We have seen that Code First creates a database automatically in the Simple Code First Examplesection. Here, we will learn how Code first decides the database name and server while initializing a database.

    The following figure shows a database initialization workflow, based on the parameter passed in the base constructor of context class, which is derived from DbContext:

    Entity Framework code-first database initialization

    As per the above figure, base constructor of the context class can have the following parameter.

    1. No Parameter
    2. Database Name
    3. Connection String Name

    No Parameter:

    If you do not specify the parameter in the base constructor of the context class then it creates a database in your local SQLEXPRESS server with a name that matches your {Namespace}.{Context class name}. For example, Code First will create a database named SchoolDataLayer.Context for the following context class:

    namespace SchoolDataLayer
    {
        public class Context: DbContext 
        {
            public Context(): base()
            {
                
            }
        }
    }

    Database Name:

    You can also specify the database name as a parameter in a base constructor of the context class. If you specify a database name parameter, then Code First creates a database with the name you specified in the base constructor in the local SQLEXPRESS database server. For example, Code First will create a database named MySchoolDB for the following context class.

    namespace SchoolDataLayer
    {
        public class Context: DbContext 
        {
            public Context(): base("MySchoolDB") 
            {
                       
            }
        }
    }

    ConnectionString Name:

    You can also define connection string in app.config or web.config and specify connection string name starting with "name=" in the base constructor of the context class. Consider the following example where we pass name=SchoolDBConnectionString parameter in the base constructor.

    namespace SchoolDataLayer
    {
        public class Context: DbContext 
        {
            public SchoolDBContext() : base("name=SchoolDBConnectionString") 
            {
            }
        }
    }

    App.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <connectionStrings>
        <add name="SchoolDBConnectionString" 
        connectionString="Data Source=.;Initial Catalog=SchoolDB-ByConnectionString;Integrated Security=true" 
        providerName="System.Data.SqlClient"/>
        </connectionStrings>
    </configuration>

    In the above context class, we specify a connection string name as a parameter. Please note that connection string name should start with "name=" otherwise, it will consider it as a database name. The database name in the connection string in app.config is SchoolDB-ByConnectionString. Code-First will create a new SchoolDB-ByConnectionString database or use existing SchoolDB-ByConnectionString database at local SQL Server. Make sure that you include providerName = "System.Data.SqlClient" in the connection string.

    Thus, Code-First use the base constructor parameter to initialize a database.

  • 相关阅读:
    npm, node, pm2 使用笔记
    没加证书的域名通过https访问,错误的访问到有证书的域名项目--已解决
    mysql数据库大表加索引
    上传大文件失败
    ifame 与父页面进行数据交互(跨域)
    windows平台编译PHP及扩展 和 踩过的坑
    vim 使用笔记
    git 在pull/push指定密钥文件
    记一次使用Xshell登陆提示所选用户密钥未在远程主机上注册
    学习网站与参考文档
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5644070.html
Copyright © 2011-2022 走看看