EntityFramework,是Microsoft的一款ORM(Object-Relation-Mapping)框架。同其它ORM(如,NHibernate,Hibernate)一样,
一是为了使开发人员以操作对象的方式去操作关系型数据表。
二是为了屏蔽底层不同厂商的数据库,开发人员面向ORM框架编写数据的CRUD(Create,Retrieve,Update,Delete)操作,再由ORM框架将这些操作翻译成不同数据库厂商的语言。
从EF 4.X开始支持三种构建方法:1. Database First方法。2.Model First方法。3.Code First 方法。
本次测试以Visual Studio / MS Sql Server / Entity Framework 6.X 测试EF
Code First Demo【此时数据库和表格都已经存在,为了在原有项目中引入EF,需要使用这种方式】
【关键:上下文,实体类的约束及关系】
//MS SQL Server连接字符串 //connectionString="Data Source=(local);Initial Catalog=EFTest;User Id=sa;Password=123456;" providerName="System.Data.SqlClient"
<connectionStrings><add name="EFTest" connectionString="Data Source=(local);Initial Catalog=EFTest;User Id=sa;Password=123456;" providerName="System.Data.SqlClient"></add></connectionStrings>
原有项目中,已经有了模型类,所以不再重新生成模型类
操作步骤:
1>引入程序集EntityFramework.dll,System.Data.Entity.dll
2>在配置文件中 写连接字符串
3>创建模型类(如果项目中有模型类,则只需要维护关系)
通过导航属性来表示类的关系,注意:导航属性设置成virtual,
特性维护:Table,Key,ForeignKey
4>创建上下文类,继承自DbContext
调用父类构造方法,传递连接字符串"name=***"
5>根据类型创建数据库表
Context1 context = new Context1();
使用context.Database.CreateIfNotExists();完成数据库中表的创建;
调用context.SaveChanges()方法完成保存。
1:打开SQLServer,使用下面SQL文本创建MyFirstModelFirstEF数据库
create database EFTeston primary(name='EFTest.mdf',--修改为自己电脑上SQL DB路径filename='D:\yangZ_MSSQL\EFTest.mdf',size=5mb,maxsize=100mb,filegrowth=10%)log on(name='EFTest_log.ldf',--修改为自己电脑上SQL DB路径filename='D:\yangZ_MSSQL\EFTest_log.ldf',size=2mb,maxsize=100mb,filegrowth=5mb)go
2:新建ConsoleApplication应用程序EFCodeFirstDemo
3:引入程序集EntityFramework.dll,System.Data.Entity.dll (默认找不到引用EntityFramework.dll)
3.1:可以通过NuGet来获取 [工具-->库程序包管理器-->程序包管理器控制台],Ps:这种方式要求电脑必须联网
Install-Package EntityFramework -Version 6.0.2Install-Package EntityFramework -Pre (表示最新)Uninstall-Package EntityFramework -Version 6.1.0
3.2:拷贝现有EF项目中的EntityFramework.dll文件
3.3:可以通过ModelFirst/DatabaseFirst(不添加任何表格)引入dll,然后删除Model.edmx文件
在EFCodeFirstDemo上 右键-->新建-->新建项-->数据-->实体数据模型,选择从数据库生成/空模型,然后点击完成
4:在配置文件中 写连接字符串
<connectionStrings><add name="EFTest" connectionString="Data Source=(local);Initial Catalog=EFTest;User Id=sa;Password=123456;" providerName="System.Data.SqlClient"></add></connectionStrings>
5:创建模型类(如果项目中有模型类,则只需要维护关系)
通过导航属性来表示类的关系,注意:导航属性设置成virtual,
特性维护:Table,Key,ForeignKey
ContactInfo.cs
using System;using System.Collections.Generic;using ponentModel.DataAnnotations;using ponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EFCodeFirstDemo{//Table是在 ponentModel.DataAnnotations.Schema[Table("ContactInfo")]public class ContactInfo{//Key 是在ponentModel.DataAnnotations [Key]public int InfoId { get; set; }public string InfoName { get; set; }//ForeignKey 是在ponentModel.DataAnnotations.Schema[ForeignKey("ContactType")]public int ContactTypeId { get; set; }public virtual ContactType ContactType { get; set; }}}
ContactType.cs
using System;using System.Collections.Generic;using ponentModel.DataAnnotations;using ponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EFCodeFirstDemo{//Table是在 ponentModel.DataAnnotations.Schema[Table("ContactType")]public class ContactType{public ContactType(){this.ContactInfo = new HashSet<ContactInfo>();}//Key 是在ponentModel.DataAnnotations [Key]public int TypeId { get; set; }public string TypeTitle { get; set; }public virtual ICollection<ContactInfo> ContactInfo { get; set; }}}
通过实体框架 Code First 建立新数据库链接: /s/1qYBZiCc 密码: 5r4a
6:创建上下文类,继承自DbContext
调用父类构造方法,传递连接字符串"name=***"
6.1:在EFCodeFirstDemo project项目 右键-->添加-->类 Context1.cs
using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EFCodeFirstDemo{public class Context1 : DbContext{public Context1(): base("name=EFTest"){}public virtual DbSet<ContactType> ContactType { get; set; }public virtual DbSet<ContactInfo> ContactInfo { get; set; }}}
7:根据类型创建数据库表
Context1 context = new Context1();
使用context.Database.CreateIfNotExists();完成数据库中表的创建;
调用context.SaveChanges()方法完成保存。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EFCodeFirstDemo{class Program{static void Main(string[] args){Context1 context = new Context1();//在现有链接数据库中 ,若当前实体模型对应的数据库不存在,则创建,反之则不需要创建 context.Database.CreateIfNotExists();context.SaveChanges();}}}
此时对应MS SQL Server数据库为:
【未完,待续】