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.
Entity Framework Code-First(6):Database Initialization的更多相关文章
- Entity Framework Tutorial Basics(13):Database First
Database First development with Entity Framework: We have seen this approach in Create Entity Data M ...
- Entity Framework Code first(转载)
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...
- Entity Framework Code First (三)Data Annotations
Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...
- Entity Framework Code First (二)Custom Conventions
---------------------------------------------------------------------------------------------------- ...
- Entity Framework Code First (一)Conventions
Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...
- Entity Framework Tutorial Basics(4):Setup Entity Framework Environment
Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...
- Entity Framework Tutorial Basics(11):Code First
Code First development with Entity Framework: Entity Framework supports three different development ...
- Entity Framework Code First (七)空间数据类型 Spatial Data Types
声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...
- Entity Framework Code First (四)Fluent API - 配置属性/类型
上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...
- Entity Framework Code First (八)迁移 Migrations
创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...
随机推荐
- 【leetcode刷题笔记】Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- alpine安装更新
apk search libaio apk add make cmake libaio-dev libffi-dev glib-dev glib
- 某国际知名IT公司笔试
原文地址:http://blog.csdn.net/lazy_tiger/article/details/1790986 这段时间没怎么顾及自己的这个“一寸土地”, 实在惭愧.因为这些天小弟又经历了“ ...
- 监控MySQL的脚本
#!/bin/bash res=`netstat -lntup | grep | awk -F '[ :]+' '{print $5}'` " ];then echo "Mysql ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...
- LINQ 学习路程 -- 查询操作 Distinct Except Intersect Union
Set Operators Usage Distinct 去掉集合的重复项 Except 返回两个集合的不同,第一个集合的元素不能出现在第二个集合中 Intersect 返回两个集合的交集,即元素同时 ...
- Java的TCP网络编程
服务端代码: package socket; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...
- GridView行中按钮的使用
转载自:http://blog.csdn.net/hongdi/article/details/6455947 GridView行中按钮的使用 在web项目的过程中,特别是开发ASP.NET应用程序, ...
- cpu满问题分析
功能问题,通过日志,单步调试相对比较好定位. 性能问题,例如线上服务器CPU100%,如何找到相关服务,如何定位问题代码,更考验技术人的功底. 做为开发人员,肯定会遇到这类问题,介绍一下分析CPU 1 ...
- 设计模式 之 《观察者模式 (Observer)》
#ifndef __OBSERVER_MODEL__ #define __OBSERVER_MODEL__ #include <string> #include <iostream& ...