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.

Entity Framework Code-First(6):Database Initialization的更多相关文章

  1. Entity Framework Tutorial Basics(13):Database First

    Database First development with Entity Framework: We have seen this approach in Create Entity Data M ...

  2. Entity Framework Code first(转载)

    一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...

  3. Entity Framework Code First (三)Data Annotations

    Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...

  4. Entity Framework Code First (二)Custom Conventions

    ---------------------------------------------------------------------------------------------------- ...

  5. Entity Framework Code First (一)Conventions

    Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...

  6. 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 ...

  7. Entity Framework Tutorial Basics(11):Code First

    Code First development with Entity Framework: Entity Framework supports three different development ...

  8. Entity Framework Code First (七)空间数据类型 Spatial Data Types

    声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...

  9. Entity Framework Code First (四)Fluent API - 配置属性/类型

    上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...

  10. Entity Framework Code First (八)迁移 Migrations

    创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...

随机推荐

  1. Android系统Recovery模式的工作原理【转】

    本文转载自:http://blog.csdn.net/mu0206mu/article/details/7464987  在使用update.zip包升级时怎样从主系统(main system)重启进 ...

  2. mysql 在windows server下发生系统错误 1067, 进程意外终止的解决方法

    mysql 在windows server下发生系统错误 1067, 进程意外终止,请检查系统盘下的windows目录下是否存在mysql的配置文件my.ini,如存在,将其删除或改名即可.

  3. java入门了解10

    1.IO: 1.File (一)注意: a.可以表示文件或者文件夹(也可称为目录) b.创建的File对象实际上不存在的文件只是代表了一个抽象路径 c.Windows中分隔符'\'('//'也可以); ...

  4. LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行

    延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...

  5. CDH- 集群时间同步ntp问题解决

    在CDH集群中发现有两台机器获取不到心跳(),导致监控不了机器状态,出现告警 可以使用ntpstat检查与ntp 服务器的时间偏差状态 使用 ntpstat 发现没有同步到ntp时间服务器,运行 nt ...

  6. myeclipse+maven

    1.Windows-->Preferences-->MyEclipse-->Maven4MyEclipse 2.Installations-->Add:添加Mavan 3.Us ...

  7. iframe弹出层中关闭包含iframe的div(子页面调用父页面js函数)

    父页面: <div id="win2" style=" width:300px; height:200px; border:1px solid red;" ...

  8. npm-install once

    Once 是我最习惯的模块,它展示了几乎所有的我书写的通过issac Schlueter创建的应用. 原理很简单,Once使用各类一个函数且返回了一个函数,你可以调用这个函数,但是只能调用一次.如果你 ...

  9. 并发问题引出ThreadLocal

    ThreadLocal Thread-->人类Runnable-->任务类 多线程并发问题引出ThreadLocal 多线程并发问题的原因: 操作同一个对象,对对象具有读写权限(只读如拍照 ...

  10. codeforces 653B B. Bear and Compressing(dfs)

    题目链接: B. Bear and Compressing time limit per test 2 seconds memory limit per test 256 megabytes inpu ...