zoukankan      html  css  js  c++  java
  • Database Initialization in Entity Framework 6

    We have seen that Code-First creates a database automatically in the Simple Code First Example section. Here, we will learn how EF decides the database name and server while initializing a database in code-first approach.

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

    Entity Framework code-first database initialization

    As per the above figure, the 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, EF 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 the connection string in app.config or web.config and specify the connection string name starting with "name=" in the base constructor of the context class. Consider the following example where we pass the 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 the 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. EF will create a new SchoolDB-ByConnectionString database or use the existing SchoolDB-ByConnectionString database in the local SQL Server. Make sure that you include providerName = "System.Data.SqlClient" for the SQL Server database in the connection string.

  • 相关阅读:
    elasticsearch ——id字段说明,内部是_uid
    企业安全建设之搭建开源SIEM平台(上)
    江西鹰潭、江西移动与华为战略合作:共推物联网——物联网的世界要到来了
    Luke 5—— 可视化 Lucene 索引查看工具,可以查看ES的索引
    Apache Flink vs Apache Spark——感觉二者是互相抄袭啊 看谁的好就抄过来 Flink支持在runtime中的有环数据流,这样表示机器学习算法更有效而且更有效率
    转:shell比较两个字符串是否相等
    UNIX 缩写风格
    转:.Net程序员学习Linux最简单的方法
    asp.net插入sql server 中文乱码问题解决方案
    asp.net将object或string转为int
  • 原文地址:https://www.cnblogs.com/cxxtreasure/p/13509300.html
Copyright © 2011-2022 走看看