ORM框架之Entity Framework
1、什么是orm
即Object-Relationl Mapping,直译过来就是对象关系映射。作用呢就是在关系型数据库和对象之间做一个映射,这样我们就能直接操作对象就能完成对数据库的增删改查。其本质就是通过对象的变化自动生成对应的sql并执行,方便。
2、Entity Framework是什么?
Entity Framework是一种微软开发的orm框架。该框架有三种模式,分别是:DataBase First 数据库优先, 就是先设计数据库,创建好数据库映射成对象和上下文;Model First 模型优先,就是设计EDM模型然后生成到数据库和创建对象和上下文;CodeFirst 代码优先,手动创建POCO模型,数据层DbContext及映射关系,通过Database.SetInitializer生成数据库,这种方式较灵活,但是需要手动定义实体模型和关系映射。我们这里用到的是CodeFirst 。
3、引用ef框架并根据已有数据库创建数据库对象和上下文
1)、在使用ef的Data项目和webconfig所在的项目中,分别使用NuGet管理引入MySql.Data.Entity框架,自己根据版本选择安装,我是最新版。
2)、在Data项目添加新项,选择ADO.NET 实体数据模型
3)、选择空CodeFirst模型
4)、然后就是webconfig的配置,webconfig所在项目添加了MySql.Data.Entity的话,应该会自动添加配置,如果没有,就需要手动更新以下的配置:
1 <configuration> 2 <configSections> 3 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 4 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 5 </configSections> 6 <entityFramework> 7 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 8 <parameters> 9 <parameter value="mssqllocaldb" /> 10 </parameters> 11 </defaultConnectionFactory> 12 <providers> 13 <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 14 <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.10.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"> 15 </provider> 16 </providers> 17 </entityFramework> 18 <system.data> 19 <DbProviderFactories> 20 <remove invariant="MySql.Data.MySqlClient" /> 21 <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.10.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> 22 </DbProviderFactories> 23 </system.data> 24 </configuration>
添加数据库连接字符串:
1 <add name="SnailFis" connectionString="Database='xxxx';Data Source='192.168.1.1';User Id='admin';Password='123456';charset='utf8';pooling=true" providerName="MySql.Data.MySqlClient" />
5)、修改SnailFisDbContext
public class SnailFisDbContext : DbContext { public SnailFisDbContext() : base("name=SnailFis") { //解决团队开发中,多人迁移数据库造成的修改覆盖问题。 Database.SetInitializer<SnailFisDbContext>(null); } public virtual DbSet<DicDbModel> DicList { get; set; } }
6)、创建DicDbModel类
/// <summary> /// 字典信息基类 /// </summary> [Table("bt_dic")] public class DicDbModel { /// <summary> /// 蜗居id /// </summary> [key] [DBColumn("SfId")] public int SfId { get; set; } /// <summary> /// 字典id /// </summary> [DBColumn("DicId")] public int DicId { get; set; } /// <summary> /// 字典名称 /// </summary> [DBColumn("DicName")] public string DicName { get; set; } }
4、ef的增删改查
1)、增
using (var snailFisDbContext = new SnailFisDbContext()) { snailFisDbContext.DicList.Add(new DicDbModel() {SfId=1,DicId=1,DicName="新华字典" }); snailFisDbContext.DicList.Add(new DicDbModel() {SfId=2,DicId=2,DicName="新华大字典" }); snailFisDbContext.SaveChanges(); }
2)、查
var snailFisDbContext = new SnailFisDbContext() var tempList = snailFisDbContext.DicList.Where(v => v.SfId > 1).ToList();
3)、删
using (var snailFisDbContext = new SnailFisDbContext()) { var sql = snailFisDbContext.DicList.SqlQuery("select * from [DicList] where [SfId] > 1"); snailFisDbContext.DicList.RemoveRange(sql); var delList = snailFisDbContext.DicList.Where(v=>v.SfId>1); snailFisDbContext.DicList.RemoveRange(delList); snailFisDbContext.SaveChanges(); }
4)、改
using (var snailFisDbContext = new SnailFisDbContext()) { var tempModel = snailFisDbContext.DicList.FirstOrDefault(v => v.SfId == 1);//修改需要把对应的数据先拿出来 if (tempModel != null) { tempModel.DicName = "新华词典"; } var updateList = snailFisDbContext.DicList.Where(v => v.SfId > 1);//当然也可以批量修改 updateList.ToList().ForEach(v => { v.DicName = "待定"; }); snailFisDbContext.SaveChanges(); }
5、ef的批量操作
EntityFramework.Utilities