EF 数据库连接约定(Connection String Conventions in Code First)
一个典型的EF应用大多数情况下是一个DbContext的派生类(derived class)来控制,通常可以使用该派生类调用DbContext的构造函数,来控制以下的东西:
(1)、上下文如何连接到数据库(给定连接字符串)
(2)、上下文是通过Code First语法计算模型还是使用EF 设计器
(3)、额外的高级选项
下面是DbContext构造器的常用的用途:
一、DbContext无参构造函数
如果当前EF应用中没有做任何的配置.且在你自定义的数据库上下文类中没有调用DbContext带参的构造函数,那么当前应用对应的数据库上下文类,将会调用DbContext的默认无参的构造函数(EF默认规定的数据库连接),代码如下:
namespace Demo.EF
{
public class EFStudyContext : DbContext
{
public EFStudyContext()// C# will call base class parameterless constructor by default
{
}
}
}
EF默认的联结字符串如下:
Data Source=(localdb)\mssqllocaldb;Initial Catalog=EFStudyConsole(项目名称).EFStudyDbContext(上下文名称);Integrated Security=True;MultipleActiveResultSets=True
EF会用上下文的完全限定名(命名空间+上下文类)作为数据库名,创建一个连接字符串,该连接字符串会连接本地的SQL Express或者LocalDb,并在SQL Express或者LocalD创建对应的数据库,如果两者都安装了,则会选择连接SQL Express。
注:VS2010默认安装SQL Express,VS2012默认安装LocalDb,在安装过程中,EF NuGet包会检查哪个数据库服务(前面介绍的)可用,当EF创建默认连接的时候,当EF创建默认链接的时候,NuGet包将通过设置默认的Code First数据库服务器来更新配置文件,该数据库服务器在通过约定创建连接时首先使用该服务器。.如果SQL Express 正在运行,它会被使用,如果它不可用,LocalDb会替代它,但是这个过程不会对配置文件做任何的更改,如果它已经包含默认连接工厂的设置.
二、DbContext带string参数的构造函数
1、如果没有在数据库上下文进行其他额外的配置,然后调用DbContext中的带参的构造函数,传入你想要使用的数据库连接字符串,然后Code First中的数据库上下文就会运行在基于当前数据库连接字符串上.代码如下:
public class BloggingContext : DbContext
{
public BloggingContext(): base("BloggingDatabase")
{}
}
Data Source=(localdb)\mssqllocaldb;Initial Catalog=BloggingDatabase;Integrated Security=True;MultipleActiveResultSets=True
2、使用app.config/web.config配置文件中的连接字符串,表示你在应用程序中已经进行了配置,这一点要区分上面的方法.
(1)、有Ado.Net使用经历的都知道,一般情况下,数据库连接字符串一般定义在app.config/web.config配置文件中,例如:
<configuration>
<connectionStrings>
<add name="BolggingContext"
providerName="System.Data.SqlServerCe.4.0"
connectionString="Data Source=Blogging.sdf"/>
</connectionStrings>
</configuration>
这在EF中相当于告诉数据库上下文去使用当前连接字符串对应的数据库服务,而不是使用SQL Express or LocalDb,数据库上下文代码如下:
public class BloggingContext : DbContext
{
public BloggingContext()
{
}
}
如果连接字符串的name属性值和上下文类名一样(either with or without namespace qualification),那么数据库上下文在执行无参构造函数的时候,会使用配置文件的连接字符串去连接数据库.
using (var context=new BloggingContext())
{
string connStr = context.Database.Connection.ConnectionString;
Console.WriteLine(connStr);
}
(2)、如果连接字符串的name属性值和上下文类名不一样,但是还是希望上下文使用配置文件的数据库连接进行数据库连接,这时就需要在上下文构造函数中调用DbContext的带string参数的构造函数,并传入连接字符串的name属性值,代码如下:
public class BloggingContext:DbContext
{
public DbSet<User> Users { get; set; } public BloggingContext():base("BloggingStr")
{ }
}
static void Main(string[] args)
{
using (var context=new BloggingContext())
{
string connStr = context.Database.Connection.ConnectionString;
Console.WriteLine(connStr);
}
Console.ReadKey();
}
另外一种方式是传递给DbContext构造函数配置文件中的connectionString节点的name属性来指定上下文通过配置文件中connectionString来连接字符串,代码如下:
public class BloggingContext:DbContext
{
public DbSet<User> Users { get; set; } public BloggingContext():base("name=BloggingStr")
{ }
}
static void Main(string[] args)
{
using (var context=new BloggingContext())
{
string connStr = context.Database.Connection.ConnectionString;
Console.WriteLine(connStr);
}
Console.ReadKey();
}
上面这种方式是明确EF进行数据库连接的时候去配置文件找连接字符串。
(3)、连接字符串的终极解决方案,直接给连接字符串,什么都不要配,代码如下:
public class BloggingContext:DbContext
{
public DbSet<User> Users { get; set; } public BloggingContext():base("server=.;database=EFStudy;uid=sa;pwd=123456;")
{ }
}
static void Main(string[] args)
{
using (var context=new BloggingContext())
{
string connStr = context.Database.Connection.ConnectionString;
Console.WriteLine(connStr);
}
Console.ReadKey();
}
注:默认情况下,当前的连接字符串使用的是System.Data.SqlClilent作为provider,这里可以被改变通过做一个IConnectionFactory的不同的实现来替换context.Database.DefaultConnectionFactory默认的实现.
三、还有其他两种方法,不常用
1、You can use an existing DbConnection object by passing it to a DbContext constructor. If the connection object is an instance of EntityConnection, then the model specified in the connection will be used rather than calculating a model using Code First. If the object is an instance of some other type—for example, SqlConnection—then the context will use it for Code First mode.
使用一个DbConnection 实例,或者是SqlConnection实例或者EntityConnection实例,传递给DbContext的构造函数均可指定对应的数据库连接规则.
2、You can pass an existing ObjectContext to a DbContext constructor to create a DbContext wrapping the existing context. This can be used for existing applications that use ObjectContext but which want to take advantage of DbContext in some parts of the application.
EF 数据库连接约定(Connection String Conventions in Code First)的更多相关文章
- ORM系列之二:EF(4) 约定、注释、Fluent API
目录 1.前言 2.约定 2.1 主键约定 2.2 关系约定 2.3 复杂类型约定 3.数据注释 3.1 主键 3.2 必需 3.3 MaxLength和MinLength 3.4 NotMapped ...
- MVC: Connection String
背景: 之前项目使用的是DB first/Model first,现在要对EF升级的6.0,并且更换成Code first. 问题: 1. System.Data.Entity.Core.Metada ...
- Entity Framework Connection String不保留密码的方法
添加Entity Data Model的时候,到最后一步,有两个radio box: 如果选择include sensitive data,虽然很方便,但是在web.config或者app.confi ...
- MVC模式下unity配置,报错“No connection string named '**Context' could be found in the application config file”
写在前面: 第一次配置时好好的,后来第二次改到MVC模式,把依赖注入写成字典的单例模式时,由于新建的ORM(数据库映射模型EF),怎么弄都不用,一直报错"No connection str ...
- 转载 How to Encrypt connection string in web.config
转载原地址: https://chiragrdarji.wordpress.com/2008/08/11/how-to-encrypt-connection-string-in-webconfig/ ...
- ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB
您创建的MovieDBContext类负责处理连接到数据库,并将Movie对象映射到数据库记录的任务中.你可能会问一个问题,如何指定它将连接到数据库? 实际上,确实没有指定要使用的数据库,Entity ...
- No connection string named '***' could be found in the application config file
Code-First时更新数据库遇到妖孽问题“No connection string named '***' could be found in the application config fil ...
- How to set China Azure Storage Connection String
Configure Visual Studio to access China Azure Storage Open Visual Studio 2012, Server Explorer Add n ...
- 不支持的关键字:“provider connection string”报错信息及解决方案
今天在部署公司开发框架的时候 ,登录系统之后调用代办列表的时候就报错了 总线调用契约XX.Service.Contracts.IXXService上的GetXXCount方法时出错. Resoluti ...
随机推荐
- Python 学习目录
第一章 Python基础 第二章 Python基础 第三章 Python基础-文件操作&函数 1.python文件处理 2.py-函数基础 3.py-函数进阶 第四章 Python基础-常用模 ...
- An existing resource has been found at location D:\Tomcat 7\apache-tomcat-7.0.55\webapps。。。
这个错误是说你的资源丢失,就是说tomcat无法解析你的.class文件,需要自己重新配置一下. 解决方法: 右击项目名 ---> 点击properties --> 在搜索栏里 输入 WE ...
- 基于图像切换器(imageSwitcher)的支持动画的图片浏览器
利用GridView和ImageSwitcher的基本用法 public class MainActivity extends Activity { int[] imageIds = new int[ ...
- js之create()
语法: Object.create(proto, [propertiesObject]) 返回一个新的对象的指针 proto:对象会被作为新创建的对象的原型 [propertiesObject]:对象 ...
- Vue 需要使用jsonp解决跨域时,可以使用(vue-jsonp)
1,执行命令 npm install vue-jsonp --save 2.src/main.js中添加: import VueJsonp from 'vue-jsonp' Vue.use(VueJs ...
- WriteableBitmap(一)
通常,WPF中的位图是不可变的.不可变的位图非常有效,如果您希望进行大量的动态更改,创建和销毁它们的开销会变得非常昂贵.在这种情况下,您需要一些更灵活的东西——WriteableBitmap. Wri ...
- HDU 1166敌兵布阵 2016-09-14 18:58 89人阅读 评论(0) 收藏
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- Android-LoaderManager异步加载数据库数据
LoaderManager异步加载数据库数据,是在(Activity/fragment/其他UI等) 加载大量的本地Database库表数据,由于数据大在加载过程中会导致UI线程阻塞,导致用户体验不好 ...
- 用CSS3把列表项目反转显示
忘了哪儿的一个题目来着,说是把 一个列表 给翻转序列显示,比如 : 有一个列表如图: 翻转为 回复里面有人机智的使用 CSS3 的 transform:rotate(180deg); 实现了,引发众 ...
- 在c#客户端程序中使用log4net
为什么使用log4net 有些日志语句只是在开发中用于调试的,不应该在Release版本中输出,log4net通过配置文件可以为Debug和Release不同的模式设置不同的输出级别来控制,而且如果已 ...