EF框架的简单概述

实体框架ADO.NET Entity Framework (EF)

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


实体数据模型(Entity Data Model) (EDM)

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


概念架构定义语言 Conceptual schema definition language (csdl)

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


存储架构定义语言 Store schema definition language (ssdl)

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


映射规范语言 Mapping specification language (msl)

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


ADO.NET Entity Framework分Storage Provider ,Mapping Layer ,Object Services,LINQ to Entities 四层
Storage Provider:负责直接和数据源通讯,支持的数据库Sql Server
Mapping Layer:数据库概念层和逻辑层的映射。通过EDM模型和mapping provider,应用程序将构建在更高层次的EDM模型抽象层次上。同时,在应用程序中将不再使用本地数据库的查询语言比如(T-sql),取而代之的将是Entity SQL。
Object Services:Object Services 的目标是消除数据和应用程序代码风格的不匹配ADO.NET允许将查询结果呈现为行和列记录,同时也可以呈现为.NET对象。该层还包括了更多被O/R mapping框架支持的高级的服务,比如身份认证,跟踪对象状态变化,并行性检查以及处理更新。
LINQ to Entities:将Entity Framework与LINQ项目集成,以提供面向对象编程语言适合自己特点的查询功能。 
LINQ to Entities这一层依赖于object services和mapping layer这两层。
ADO.NET Entity Framework 的数据访问方式与ADO.NET 有类似之处 
 ADO.NET ADO.NET Entity Framework
SqlConnection EntityConnection
SqlCommand EntityCommand
SqlDataReader. EntityDataReader
SqlDataAdapter ObjectContext
              ObjectQuery<T>
DataSet       Entity
       csdl,ssdl,msl
注意:ADO.NET与    ADO.NET Entity Framework是不同的技术,这个对比只是一个帮助理解的比效
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.EntityClient;
using System.Data.Objects;
namespace MyWindowsForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = GetAllCustomerByObjectContextNOESql();
        }
        //Ado.net方式
        private List<Customers> GetAllCustomer()
        {
            List<Customers> customerList=new List<Customers> ();
            using (EntityConnection econ = new EntityConnection(@"metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=wy;MultipleActiveResultSets=False'"))
            {
                EntityCommand ecmd = new EntityCommand("select value it from NorthwindEntities.Customers as it", econ);
                econ.Open();
                EntityDataReader edr=ecmd.ExecuteReader(CommandBehavior.SequentialAccess);
                while (edr.Read())
                {
                    Customers customer = new Customers { 
                        Address=edr["Address"].ToString(),
                        City = edr["City"].ToString(),
                        Country = edr["Country"].ToString(),
                        Phone = edr["Phone"].ToString()
                       };
                    customerList.Add(customer);
                }
            }
            return customerList;
        }
        //ObjectContext方式 Ado.net DataAdapter DataSet方式
        private IEnumerable<Customers> GetAllCustomerByObjectContext()
        {
            EntityConnection econ = new EntityConnection(@"metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=wy;MultipleActiveResultSets=False'");
            ObjectContext context = new ObjectContext(econ);//相当于DataAdapter,会自动打开数据连接
            ObjectQuery<Customers> objquery = context.CreateQuery<Customers>("select value it from NorthwindEntities.Customers as it where it.CustomerID='cccc'");//相当于DataSet
            return objquery.AsEnumerable<Customers>();
        }
        //不用ESql的方法
        private IEnumerable<Customers> GetAllCustomerByObjectContextNOESql()
        {
            NorthwindEntities northData = new NorthwindEntities();
            return northData.Customers.Where(s => s.Region == null).AsEnumerable<Customers>();
        }
        //Linq及 '入'表达式
        private IEnumerable<Customers> GetAllCustomerByLinq()
        {
            NorthwindEntities northData = new NorthwindEntities();
            var query = from customer in northData.Customers
                        select customer;
            return query.Where(s => s.Region != null).AsEnumerable<Customers>();//Where(s => s.Region != null)为‘入’表达式可以加Where条件来控制数据的显示
        }
 private void btnNew_Click(object sender, EventArgs e)//增加数据
        {
            NorthwindEntities northData = new NorthwindEntities();
            Customers customer = new Customers { 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());
        }
        private void btnDelete_Click(object sender, EventArgs e)//删除数据
        {
            NorthwindEntities northData = new NorthwindEntities();
            Customers customer = 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());
        }
        
        private void btnChange_Click(object sender, EventArgs e)//修改数据
        {
            NorthwindEntities northData = new NorthwindEntities();
            Customers customer = northData.Customers.First(s => s.CustomerID == "wy");
            MessageBox.Show(customer.EntityState.ToString());
            customer.CompanyName = "Hello World";
            MessageBox.Show(customer.EntityState.ToString());
            northData.SaveChanges();
            MessageBox.Show(customer.EntityState.ToString());
        }
    }
}



展开阅读全文

没有更多推荐了,返回首页