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:

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.

最新文章

  1. centos7.0 安装redis集群
  2. ubuntu 下安装mysql,以及配置远程登录
  3. NDK学习4: Eclipse HelloWorld
  4. shinydashboard包---为shiny提供BI框架
  5. xml版本学生管理系统
  6. 精心挑选的12款优秀 jQuery Ajax 分页插件和教程
  7. tomcat配置文件server.xml详解 转载http://blog.csdn.net/yuanxuegui2008/article/details/6056754
  8. 你真的了解:IIS连接数、IIS并发连接数、IIS最大并发工作线程数、应用程序池的队列长度、应用程序池的最大工作进程数 吗?
  9. Java 集合的简单实现 (ArrayList &amp; LinkedList &amp; Queue &amp; Stack)
  10. 使用Docker+Jenkins自动构建部署
  11. SpringBoot系列: JdbcTemplate 事务控制
  12. 简析服务端通过geotools导入SHP至PG的方法
  13. 学习windows编程 day4 之 自定义映射
  14. 利用 SPL 快速实现 Observer 设计模式
  15. GoLand tool tips
  16. C++11--正则表达式&lt;regex&gt;
  17. Python模块学习 - jinja2
  18. arcgis python 沿线生成点
  19. MySQL数据库行去重复
  20. Kotlin Reference (七) Returns and Jumps

热门文章

  1. 【leetcode刷题笔记】Max Points on a Line
  2. 手机端适配rem代码片段
  3. LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
  4. JVM中垃圾回收算法
  5. c版基于链表的插入排序(改进版)
  6. php 简单笔记
  7. 序列化工具类({对实体Bean进行序列化操作.},{将字节数组反序列化为实体Bean.})
  8. 分享知识快乐自己:Layui 常用样式
  9. LXC linux容器简介——在操作系统层次上为进程提供的虚拟的执行环境,限制其使用的CPU和mem等资源,底层是linux内核资源管理的cgroups子系统
  10. Java_数据交换_Gson_00_资源帖