EF框架.docx

上传人:b****4 文档编号:24461604 上传时间:2023-05-27 格式:DOCX 页数:7 大小:16.06KB
下载 相关 举报
EF框架.docx_第1页
第1页 / 共7页
EF框架.docx_第2页
第2页 / 共7页
EF框架.docx_第3页
第3页 / 共7页
EF框架.docx_第4页
第4页 / 共7页
EF框架.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

EF框架.docx

《EF框架.docx》由会员分享,可在线阅读,更多相关《EF框架.docx(7页珍藏版)》请在冰豆网上搜索。

EF框架.docx

EF框架

EF框架

实体框架ADO.NETEntityFramework(EF)

一套支持面向数据的软件应用程序开发的技术,可让开发人员使用映射到数据源中的逻辑架构的概念模型。

实体数据模型(EntityDataModel)(EDM)

一个数据模型,用于将应用程序数据定义为公共语言运行时类型和存储结构可以映射到的实体和关系集。

概念架构定义语言Conceptualschemadefinitionlanguage(csdl)

一种基于XML的语言,可用于定义概念模型的实体类型、关联、实体容器、实体集和关联集(实体)-->业务对象

存储架构定义语言Storeschemadefinitionlanguage(ssdl)

一种基于XML的语言,用于定义存储模型的实体类型、关联、实体容器、实体集和关联集,经常对应于数据库架构。

(数据存储模型)

映射规范语言Mappingspecificationlanguage(msl)

一种基于XML的语言,可用于将概念模型中定义的项映射到存储模型中的项.(所以不用去管如何存储数据的)

ADO.NETEntityFramework分StorageProvider,MappingLayer,ObjectServices,LINQtoEntities四层

StorageProvider:

负责直接和数据源通讯,支持的数据库SqlServer

MappingLayer:

数据库概念层和逻辑层的映射。

通过EDM模型和mappingprovider,应用程序将构建在更高层次的EDM模型抽象层次上。

同时,在应用程序中将不再使用本地数据库的查询语言比如(T-sql),取而代之的将是EntitySQL。

ObjectServices:

ObjectServices的目标是消除数据和应用程序代码风格的不匹配ADO.NET允许将查询结果呈现为行和列记录,同时也可以呈现为.NET对象。

该层还包括了更多被O/Rmapping框架支持的高级的服务,比如身份认证,跟踪对象状态变化,并行性检查以及处理更新。

LINQtoEntities:

将EntityFramework与LINQ项目集成,以提供面向对象编程语言适合自己特点的查询功能。

LINQtoEntities这一层依赖于objectservices和mappinglayer这两层。

ADO.NETEntityFramework的数据访问方式与ADO.NET有类似之处

ADO.NETADO.NETEntityFramework

SqlConnectionEntityConnection

SqlCommandEntityCommand

SqlDataReader.EntityDataReader

SqlDataAdapterObjectContext

ObjectQuery<T>

DataSetEntity

csdl,ssdl,msl

注意:

ADO.NET与ADO.NETEntityFramework是不同的技术,这个对比只是一个帮助理解的比效

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data.EntityClient;

usingSystem.Data.Objects;

namespaceMyWindowsForm

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

}

privatevoidForm1_Load(objectsender,EventArgse)

{

this.dataGridView1.DataSource=GetAllCustomerByObjectContextNOESql();

}

//A方式

privateList<Customers>GetAllCustomer()

{

List<Customers>customerList=newList<Customers>();

using(EntityConnectionecon=newEntityConnection(@"metadata=res:

//*/MyModel.csdl|res:

//*/MyModel.ssdl|res:

//*/MyModel.msl;provider=System.Data.SqlClient;providerconnectionstring='DataSource=.;InitialCatalog=Northwind;UserID=sa;Password=wy;MultipleActiveResultSets=False'"))

{

EntityCommandecmd=newEntityCommand("selectvalueitfromNorthwindEntities.Customersasit",econ);

econ.Open();

EntityDataReaderedr=ecmd.ExecuteReader(CommandBehavior.SequentialAccess);

while(edr.Read())

{

Customerscustomer=newCustomers{

Address=edr["Address"].ToString(),

City=edr["City"].ToString(),

Country=edr["Country"].ToString(),

Phone=edr["Phone"].ToString()

};

customerList.Add(customer);

}

}

returncustomerList;

}

//ObjectContext方式ADataAdapterDataSet方式

privateIEnumerable<Customers>GetAllCustomerByObjectContext()

{

EntityConnectionecon=newEntityConnection(@"metadata=res:

//*/MyModel.csdl|res:

//*/MyModel.ssdl|res:

//*/MyModel.msl;provider=System.Data.SqlClient;providerconnectionstring='DataSource=.;InitialCatalog=Northwind;UserID=sa;Password=wy;MultipleActiveResultSets=False'");

ObjectContextcontext=newObjectContext(econ);//相当于DataAdapter,会自动打开数据连接

ObjectQuery<Customers>objquery=context.CreateQuery<Customers>("selectvalueitfromNorthwindEntities.Customersasitwhereit.CustomerID='cccc'");//相当于DataSet

returnobjquery.AsEnumerable<Customers>();

}

//不用ESql的方法

privateIEnumerable<Customers>GetAllCustomerByObjectContextNOESql()

{

NorthwindEntitiesnorthData=newNorthwindEntities();

returnnorthData.Customers.Where(s=>s.Region==null).AsEnumerable<Customers>();

}

//Linq及'入'表达式

privateIEnumerable<Customers>GetAllCustomerByLinq()

{

NorthwindEntitiesnorthData=newNorthwindEntities();

varquery=fromcustomerinnorthData.Customers

selectcustomer;

returnquery.Where(s=>s.Region!

=null).AsEnumerable<Customers>();//Where(s=>s.Region!

=null)为‘入’表达式可以加Where条件来控制数据的显示

}

privatevoidbtnNew_Click(objectsender,EventArgse)//增加数据

{

NorthwindEntitiesnorthData=newNorthwindEntities();

Customerscustomer=newCustomers{CustomerID="wy",CompanyName="wy"};

MessageBox.Show(customer.EntityState.ToString());

northData.AddObject("NorthwindEntities.Customers",customer);

MessageBox.Show(customer.EntityState.ToString());

northData.SaveChanges();

MessageBox.Show(customer.EntityState.ToString());

}

privatevoidbtnDelete_Click(objectsender,EventArgse)//删除数据

{

NorthwindEntitiesnorthData=newNorthwindEntities();

Customerscustomer=northData.Customers.First(s=>s.CustomerID=="wy");

MessageBox.Show(customer.EntityState.ToString());

northData.DeleteObject(customer);

MessageBox.Show(customer.EntityState.ToString());

northData.SaveChanges();

MessageBox.Show(customer.EntityState.ToString());

}

privatevoidbtnChange_Click(objectsender,EventArgse)//修改数据

{

NorthwindEntitiesnorthData=newNorthwindEntities();

Customerscustomer=northData.Customers.First(s=>s.CustomerID=="wy");

MessageBox.Show(customer.EntityState.ToString());

customer.CompanyName="HelloWorld";

MessageBox.Show(customer.EntityState.ToString());

northData.SaveChanges();

MessageBox.Show(customer.EntityState.ToString());

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1