一套支持面向数据的软件应用程序开发的技术,可让开发人员使用映射到数据源中的逻辑架构的概念模型。
一个数据模型,用于将应用程序数据定义为公共语言运行时类型和存储结构可以映射到的实体和关系集。
一种基于 XML 的语言,可用于定义概念模型的实体类型、关联、实体容器、实体集和关联集(实体)-->业务对象
一种基于 XML 的语言,用于定义存储模型的实体类型、关联、实体容器、实体集和关联集,经常对应于数据库架构。(数据存储模型)
一种基于 XML 的语言,可用于将概念模型中定义的项映射到存储模型中的项.(所以不用去管如何存储数据的)
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()); } } }