博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Entity Framework Code-First(6):Database Initialization
阅读量:5895 次
发布时间:2019-06-19

本文共 2698 字,大约阅读时间需要 8 分钟。

Database Initialization:

We have seen that Code First creates a database automatically in the section. 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:

 

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.

转载于:https://www.cnblogs.com/purplefox2008/p/5644070.html

你可能感兴趣的文章
在论坛中出现的比较难的sql问题:13(循环替换问题)
查看>>
简单的Samba服务器安装
查看>>
blog addr
查看>>
如何选择 Web 前端模板引擎?
查看>>
VMware 上Clone Ubuntu虚拟机后找不到eth0
查看>>
由毫秒(ms)转换为日期和时间的格式(简单易用)
查看>>
一个女生对BootStrap的感情
查看>>
JAVA实现支付宝提现到个人账户
查看>>
操作系统指纹识别概述 - FreeBuf.COM
查看>>
Cisco 的 NAT 路由表命令
查看>>
Spring学习记录(一)
查看>>
PostgreSQL的昨天今天和明天
查看>>
JAVA算法1——连通性问题之快速查找算法
查看>>
Writing
查看>>
? VMware?添加共享磁盘?
查看>>
JAVA中int、String的类型转换
查看>>
hadoop一步步入门
查看>>
Cypher语言学习笔记
查看>>
路由协议概述(1) --- 总览
查看>>
阿里巴巴七大事业群替代子公司制
查看>>