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:
As per the above figure, base constructor of the context class can have the following parameter.
- No Parameter
- Database Name
- 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.
最新文章
- centos7.0 安装redis集群
- ubuntu 下安装mysql,以及配置远程登录
- NDK学习4: Eclipse HelloWorld
- shinydashboard包---为shiny提供BI框架
- xml版本学生管理系统
- 精心挑选的12款优秀 jQuery Ajax 分页插件和教程
- tomcat配置文件server.xml详解 转载http://blog.csdn.net/yuanxuegui2008/article/details/6056754
- 你真的了解:IIS连接数、IIS并发连接数、IIS最大并发工作线程数、应用程序池的队列长度、应用程序池的最大工作进程数 吗?
- Java 集合的简单实现 (ArrayList &; LinkedList &; Queue &; Stack)
- 使用Docker+Jenkins自动构建部署
- SpringBoot系列: JdbcTemplate 事务控制
- 简析服务端通过geotools导入SHP至PG的方法
- 学习windows编程 day4 之 自定义映射
- 利用 SPL 快速实现 Observer 设计模式
- GoLand tool tips
- C++11--正则表达式<;regex>;
- Python模块学习 - jinja2
- arcgis python 沿线生成点
- MySQL数据库行去重复
- Kotlin Reference (七) Returns and Jumps
热门文章
- 【leetcode刷题笔记】Max Points on a Line
- 手机端适配rem代码片段
- LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
- JVM中垃圾回收算法
- c版基于链表的插入排序(改进版)
- php 简单笔记
- 序列化工具类({对实体Bean进行序列化操作.},{将字节数组反序列化为实体Bean.})
- 分享知识快乐自己:Layui 常用样式
- LXC linux容器简介——在操作系统层次上为进程提供的虚拟的执行环境,限制其使用的CPU和mem等资源,底层是linux内核资源管理的cgroups子系统
- Java_数据交换_Gson_00_资源帖