EF Code First数据库连接配置
前面几节,使用的都是通过EF Code First创建的新数据库,接下来,将开始使用已存在的数据库。
1、使用配置文件设置数据库连接
App.config
数据库连接字符串的name与Data中NorthwindContext.cs类名相同
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </configSections> <connectionStrings> <add name="NorthwindContext" connectionString="Data Source=(local); Database=Northwind; User ID=sa; Password=1;" providerName="System.Data.SqlClient"/> </connectionStrings></configuration> Data中NorthwindContext.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Data.Entity; 7 8 using Northwind.Domain.Entities; 9 using Northwind.Domain.Mapping;10 11 namespace Northwind.Data12 {13 public class NorthwindContext : DbContext14 {15 public DbSet<Category> Categories { get; set; }16 public DbSet<Product> Products { get; set; }17 public DbSet<Supplier> Suppliers { get; set; }18 19 protected override void OnModelCreating(DbModelBuilder modelBuilder)20 {21 modelBuilder.Configurations.Add(new CategoryMap());22 modelBuilder.Configurations.Add(new ProductMap());23 modelBuilder.Configurations.Add(new SupplierMap());24 }25 }26 } 执行成功后,创建的数据库如下图:
2、使用构造函数指定数据库
NorthwindContext.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Data.Entity; 7 8 using Northwind.Domain.Entities; 9 using Northwind.Domain.Mapping;10 11 namespace Northwind.Data12 {13 public class NorthwindContext : DbContext14 {15 public NorthwindContext()16 { }17 18 public NorthwindContext(string databaseName)19 : base(databaseName)20 {21 22 }23 24 public DbSet<Category> Categories { get; set; }25 public DbSet<Product> Products { get; set; }26 public DbSet<Supplier> Suppliers { get; set; }27 public DbSet<User> Users { get; set; }28 public DbSet<Role> Roles { get; set; }29 30 protected override void OnModelCreating(DbModelBuilder modelBuilder)31 {32 modelBuilder.Configurations.Add(new CategoryMap());33 modelBuilder.Configurations.Add(new ProductMap());34 modelBuilder.Configurations.Add(new SupplierMap());35 modelBuilder.Configurations.Add(new UserMap());36 modelBuilder.Configurations.Add(new RoleMap());37 }38 }39 } Program.cs
using (NorthwindContext db = new NorthwindContext("Northwind")){} 3、使用构造函数指定数据库连接字符串
App.config
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <configSections> 4 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 5 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 6 </configSections> 7 <connectionStrings> 8 <add name="NorthwindConnectionString" connectionString="Data Source=(local); Database=Northwind; User ID=sa; Password=1;" providerName="System.Data.SqlClient"/> 9 </connectionStrings>10 </configuration> NorthwindContext.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Data.Entity; 7 8 using Northwind.Domain.Entities; 9 using Northwind.Domain.Mapping;10 11 namespace Northwind.Data12 {13 public class NorthwindContext : DbContext14 {15 public NorthwindContext()16 : base("name=NorthwindConnectionString")17 { }18 19 public DbSet<Category> Categories { get; set; }20 public DbSet<Product> Products { get; set; }21 public DbSet<Supplier> Suppliers { get; set; }22 public DbSet<User> Users { get; set; }23 public DbSet<Role> Roles { get; set; }24 25 protected override void OnModelCreating(DbModelBuilder modelBuilder)26 {27 modelBuilder.Configurations.Add(new CategoryMap());28 modelBuilder.Configurations.Add(new ProductMap());29 modelBuilder.Configurations.Add(new SupplierMap());30 modelBuilder.Configurations.Add(new UserMap());31 modelBuilder.Configurations.Add(new RoleMap());32 }33 }34 }
EF Code First数据库连接配置的更多相关文章
- 【译】第8节---EF Code First中配置类
原文:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx 前面的章节中我们知道 ...
- EF Code First 一对多、多对多关联,如何加载子集合?
应用场景 先简单描述一下标题的意思:使用 EF Code First 映射配置 Entity 之间的关系,可能是一对多关系,也可能是多对多关系,那如何加载 Entity 下关联的 ICollectio ...
- 【Docker】 .Net Core 3.1 webapi 集成EF Code First,使用MySql进行业务操作 、配置swagger (三)
系列目录: [Docker] CentOS7 安装 Docker 及其使用方法 ( 一 ) [Docker] 使用Docker 在阿里云 Centos7 部署 MySQL 和 Redis (二) [D ...
- 【EF Code First】 一对一、一对多的多重关系配置
这里使用相册Album和图片Picture的关系做示例 1,Album与Picture最基本的关系是1-n(一个相册可以有多张图片) 这时Album.Picture实体类可以这么定义 /// < ...
- 【EF Code First】 一对多、多对多的多重关系配置
这里使用用户表(User)和项目(Project)表做示例 有这样一个需求: 用户与项目的关系是:一个用户可以发多个项目,可以参加多个项目,而项目可以有多个参与成员和一个发布者 [其中含1-n和n-n ...
- EF Code First 数据迁移配置
这里我想讲清楚code first 数据迁移的两种模式,还有开发环境和生产环境数据迁移的最佳实践. 1.1 数据迁移综述 EF Code first 虽然已经有了几种不同的数据库初始化策略,但是大部分 ...
- EF Code First 学习笔记:约定配置 Data Annotations+Fluent API
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...
- [转载]EF Code First 学习笔记:约定配置
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...
- EF Code First 学习笔记:约定配置
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...
随机推荐
- 2016CCPC杭州现场赛 B-Bomb /// tarjan缩点
题目大意: 给定n个爆破点的信息 x y r w 表示爆破点位置为 (x,y) 爆破范围是以位置为圆心 半径为r的圆 引爆这个点的代价为w 引爆某个点时 其他位置在该爆破范围内的爆破点也会被引爆 求引 ...
- LeetCode 31. Next Permutation【Medium】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [笔记]Android开发环境配置及HelloWorld程序
Android的开发须要下面四个工具: 1.JDK 2.Eclipse 3.Android SDK 4.ADT 具体功能: 1.JDK.JDK即Java Development Kit(Java开 ...
- [转]C# Eval在asp.net中的用法及作用
原文链接:http://www.cnblogs.com/Mr_JinRui/archive/2010/07/06/1772129.html Eval( " ")和Bind( &qu ...
- springmvc常用知识总结,不定期更新
1.@Controller 注解到类名上,表示该类是控制器. 2.@RequestMapping("/xxxx") 可以放在类名/方法名之上,表示访问请求该方法时的映射url.如果 ...
- Linux 常用命令:开发调试篇
前言 Linux常用命令中有一些命令可以在开发或调试过程中起到很好的帮助作用,有些可以帮助了解或优化我们的程序,有些可以帮我们定位疑难问题.本文将简单介绍一下这些命令. 示例程序 我们用一个小程序,来 ...
- opencv3.1.0 在控制台程序中报错:winnt.h(6464): error C2872: ACCESS_MASK: 不明确的
在winnt.h里面有一个cv的命名空间,同样定义了一个ACCESS_MASK,跟opencv的cv::ACCESS_MASK发生了冲突!!! 该冲突在MFC中没有出现,在控制台程序中才会报错!对于o ...
- 关于jquery ajax项目总结
$.ajax({ url:"../action/BorrowAction.php?action=oready", async:true,//异步请求 ...
- Windows cd
显示当前目录名或改变当前目录. CHDIR [/D] [drive:][path]CHDIR [..]CD [/D] [drive:][path]CD [..] .. 指定要改成父目录. 键入 C ...
- (转)Android中px与dip,sp与dip等的转换工具类
功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方法中的参数请参考http://www.cnblogs.co ...