EF(EntityFrameWork) ORM(对象关系映射框架/数据持久化框架),根据实体对象操作数据表中数据的一种面向对象的操作框架,底层也是调用ADO.NET
ASP.NET MVC 项目会自动导入MVC程序集,因为默认.NET环境(GAC)中没有这个程序集
1: EF数据上下文 继承于 DbContext ; DBQuery<T> ,支持延迟加载:只有当使用到数据的时候,才去查询数据库; 主要成员:表映射的实体集合 Set<T>,Entry<T>()
context.Model -->Dbset<Model>
context.set<Model>() -->Dbset<Model>
//使用状态版的时候,执行修改/删除的时候必须指定主键。
2: EF中会为每个 管理的 实体对象 创建一个代理包装类对象,其中会跟踪 实体对象 的状态和每个属性的状态;
3: [EF对象管理容器]: 每个通过EF数据上下文操作的实体对象,都需要存在上下文的容器中,一旦通过上下文的某个方法操作了实体对象后,那么上下文就会给它加一个状态标识。
但调用上下文的SaveChanges方法的时候,上下文就会遍历容器中的所有对象,并检查他们的状态标识,并依照标识的值进行相应的增删改查sql操作
一、通常使用EF更新的方式,先查询出要修改的数据,然后再修改新的值;实体对象被修改的属性 在 代理包装类对象里 的对应属性状态会被修改记录下修改状态,
等到调用SaveChanges时,EF会遍历其管理的每个实体对象,并根据其 包装类对象 的状态,生成增删改查sql语句并执行;
此例中修改操作,会生成 修改的sql语句(注意:此处只为修改过的属性生成sql语句),最后执行。

1 create database MyFirstEF
2 on primary
3 (
4 name='MyFirstEF.mdf',
5 --修改为自己电脑上SQL DB路径
6 filename='E:\ProgramMSSQLServerDB\MyFirstEF.mdf',
7 size=5mb,
8 maxsize=100mb,
9 filegrowth=10%
10 )
11 log on
12 (
13 name='MyFirstEF_log.ldf',
14 --修改为自己电脑上SQL DB路径
15 filename='E:\ProgramMSSQLServerDB\MyFirstEF_log.ldf',
16 size=2mb,
17 maxsize=100mb,
18 filegrowth=5mb
19 )
20 go
21
22 use MyFirstEF
23 go
24
25 create table CustomerInfo
26 (
27 id int identity(1,1) primary key,
28 customerName nvarchar(100) not null,
29 customerDate datetime
30 )
31 go
32
33 insert into CustomerInfo values('aaaaa',getdate())
34 go
35
36 select * from CustomerInfo
37 go
38
39 create table OrderInfo
40 (
41 id int identity(1,1) primary key,
42 orderName nvarchar(100),
43 customerId int
44 )
45 go
46
47
48 alter table OrderInfo
49 add constraint FK_OrderInfo_CustomerInfo
50 foreign key(customerId) references CustomerInfo(id)
51 on delete cascade
52 on update cascade
53
54 go
55
56
57 select * from CustomerInfo
58 select * from OrderInfo
//EF context 对象 MyFirstEFEntities context = new MyFirstEFEntities(); //update //1:先查询要修改的原数据 CustomerInfo customer = context.CustomerInfoes.Find(1); //2:设置修改后的值 customer.customerDate = DateTime.Now; //3:更新到数据库 context.SaveChanges();
二、为避免先查询数据库,可以直接将 被修改的实体对象 添加到 EF中管理(此时为附加状态Attached),并手动设置其为未修改状态(Unchanged),
同时设置被修改的实体对象的包装类对象 对应属性为修改状态。
//EF context 对象 DbContext contextState = new MyFirstEFEntities(); //update CustomerInfo customerState = new CustomerInfo(); customerState.id = 1; customerState.customerDate = DateTime.Now; customerState.customerName = "bbb"; //1: 标记当前对象,必须把必填字段都填写,否则会报错:System.Data.Entity.Validation.DbEntityValidationException //1: 若此时未更新 非必填字段,则数据库会把非必填字段更新为null contextState.Entry<CustomerInfo>(customerState).State = System.Data.EntityState.Modified; contextState.SaveChanges();
//EF context 对象
DbContext contextState = new MyFirstEFEntities();
//update 2
CustomerInfo customerState = new CustomerInfo();
customerState.id = 1;
customerState.customerName = "dfdfdfdf";
//2: 针对某个属性,进行状态跟踪设置
//** 2.1: 如果使用 Entry 附加 实体对象到数据容器中,则需要手动 设置 实体包装类的对象 的 状态为 Unchanged**
//** 2.1: entry.State = System.Data.EntityState.Unchanged;
DbEntityEntry<CustomerInfo> entry = contextState.Entry<CustomerInfo>(customerState);
entry.State = System.Data.EntityState.Unchanged;
entry.Property("customerName").IsModified = true;
contextState.SaveChanges();
//EF context 对象
DbContext contextState = new MyFirstEFEntities();
//update 3
CustomerInfo customerState = new CustomerInfo();
customerState.id = 1;
customerState.customerName = "aaaaa";
//** 2.2: 如果使用 Attach 就不需要这句
//** 2.2: entry.State = System.Data.EntityState.Unchanged;
contextState.Set<CustomerInfo>().Attach(customerState);///直接针对属性进行状态设置,但是当前对象并没有被上下文跟踪
contextState.Entry<CustomerInfo>(customerState).Property("customerName").IsModified = true;
contextState.SaveChanges();
#region 多表增加操作
DbContext dbContext = new MyFirstEFEntities();
CustomerInfo customerInfo = new CustomerInfo()
{
customerName = "duobiaocaozuo",
customerDate = DateTime.Now
};
dbContext.Set<CustomerInfo>().Add(customerInfo);
OrderInfo orderInfo1 = new OrderInfo()
{
orderName = "bike1",
customerId = customerInfo.id
};
dbContext.Set<OrderInfo>().Add(orderInfo1);
OrderInfo orderInfo2 = new OrderInfo()
{
orderName = "bike2",
customerId = customerInfo.id
};
dbContext.Set<OrderInfo>().Add(orderInfo2);
dbContext.SaveChanges();
#endregion
#region 导航属性
DbContext dbContext = new MyFirstEFEntities();
CustomerInfo customerInfo = new CustomerInfo()
{
customerName = "daohangshuxing",
customerDate = DateTime.Now
};
customerInfo.OrderInfoes.Add(new OrderInfo()
{
orderName = "car1",
});
customerInfo.OrderInfoes.Add(new OrderInfo()
{
orderName = "car2"
});
dbContext.Set<CustomerInfo>().Add(customerInfo);
dbContext.SaveChanges();
#endregion

1 using System;
2 using System.Collections.Generic;
3 using System.Data.Entity;
4 using System.Data.Entity.Infrastructure;
5 using System.Linq;
6 using System.Text;
7 using System.Threading.Tasks;
8
9 namespace _20160403_MyFirstEFDemo
10 {
11 class Program
12 {
13 static void Main(string[] args)
14 {
15
16 #region 方法
17 ////EF context 对象
18 //MyFirstEFEntities context = new MyFirstEFEntities();
19 ////add
20 //CustomerInfo customer = new CustomerInfo();
21 //customer.customerName = "Test1";
22 //customer.customerDate = DateTime.Now;
23 //context.CustomerInfoes.Add(customer);
24 //context.SaveChanges();
25
26 ////update
27 ////1:先查询要修改的原数据
28 //CustomerInfo customer = context.CustomerInfoes.Find(1);
29 ////2:设置修改后的值
30 //customer.customerDate = DateTime.Now;
31 ////3:更新到数据库
32 //context.SaveChanges();
33
34 ////Read
35 //CustomerInfo customer = context.CustomerInfoes.Find(1);
36 //if (customer != null)
37 //{
38 // string strCustomerInfo = string.Format("name:{0},date:{1}", customer.customerName, customer.customerDate);
39 // Console.WriteLine(strCustomerInfo);
40 //}
41
42 ////delete
43 //CustomerInfo customer = new CustomerInfo();
44 //customer.id = 1;
45 //context.CustomerInfoes.Attach(customer);
46 //context.CustomerInfoes.Remove(customer);
47 //context.SaveChanges();
48 #endregion
49
50 #region 状态
51 //EF context 对象
52 //DbContext contextState = new MyFirstEFEntities();
53
54 ////add 1
55 //CustomerInfo customerState = new CustomerInfo();
56 //customerState.customerName = "testState1";
57 //customerState.customerDate = DateTime.Now;
58 //contextState.Set<CustomerInfo>().Add(customerState);
59 //contextState.SaveChanges();
60
61 ////add 2
62 //CustomerInfo customerState = new CustomerInfo() {
63 // customerName="stateTest111",
64 // customerDate=DateTime.Now
65 //};
66 //contextState.Entry<CustomerInfo>(customerState).State = System.Data.EntityState.Added;
67 //contextState.SaveChanges();
68
69 ////update 1
70 //CustomerInfo customerState = new CustomerInfo();
71 //customerState.id = 1;
72 //customerState.customerDate = DateTime.Now;
73 //customerState.customerName = "bbb";
74
75 ////1: 标记当前对象,必须把必填字段都填写,否则会报错:System.Data.Entity.Validation.DbEntityValidationException
76 ////1: 若此时未更新 非必填字段,则数据库会把非必填字段更新为null
77 //contextState.Entry<CustomerInfo>(customerState).State = System.Data.EntityState.Modified;
78 //contextState.SaveChanges();
79
80 ////update 2
81 //CustomerInfo customerState = new CustomerInfo();
82 //customerState.id = 1;
83 //customerState.customerName = "dfdfdfdf";
84
85 ////2: 针对某个属性,进行状态跟踪设置
86 ////** 2.1: 如果使用 Entry 附加 实体对象到数据容器中,则需要手动 设置 实体包装类的对象 的 状态为 Unchanged**
87 ////** 2.1: entry.State = System.Data.EntityState.Unchanged;
88 //DbEntityEntry<CustomerInfo> entry = contextState.Entry<CustomerInfo>(customerState);
89 //entry.State = System.Data.EntityState.Unchanged;
90 //entry.Property("customerName").IsModified = true;
91 //contextState.SaveChanges();
92
93 ////update 3
94 //CustomerInfo customerState = new CustomerInfo();
95 //customerState.id = 1;
96 //customerState.customerName = "aaaaa";
97
98 ////** 2.2: 如果使用 Attach 就不需要这句
99 ////** 2.2: entry.State = System.Data.EntityState.Unchanged;
100 //contextState.Set<CustomerInfo>().Attach(customerState);///直接针对属性进行状态设置,但是当前对象并没有被上下文跟踪
101 //contextState.Entry<CustomerInfo>(customerState).Property("customerName").IsModified = true;
102 //contextState.SaveChanges();
103
104 ////delete
105 //CustomerInfo customerState = new CustomerInfo()
106 //{
107 // id = 2
108 //};
109 //contextState.Entry<CustomerInfo>(customerState).State = System.Data.EntityState.Deleted;
110 //contextState.SaveChanges();
111
112 #endregion
113
114 #region 多表增加操作
115 //DbContext dbContext = new MyFirstEFEntities();
116
117 //CustomerInfo customerInfo = new CustomerInfo()
118 //{
119 // customerName = "duobiaocaozuo",
120 // customerDate = DateTime.Now
121 //};
122 //dbContext.Set<CustomerInfo>().Add(customerInfo);
123
124 //OrderInfo orderInfo1 = new OrderInfo()
125 //{
126 // orderName = "bike1",
127 // customerId = customerInfo.id
128 //};
129 //dbContext.Set<OrderInfo>().Add(orderInfo1);
130
131 //OrderInfo orderInfo2 = new OrderInfo()
132 //{
133 // orderName = "bike2",
134 // customerId = customerInfo.id
135 //};
136 //dbContext.Set<OrderInfo>().Add(orderInfo2);
137
138 //dbContext.SaveChanges();
139 #endregion
140
141 #region 导航属性
142 //DbContext dbContext = new MyFirstEFEntities();
143
144 //CustomerInfo customerInfo = new CustomerInfo()
145 //{
146 // customerName = "daohangshuxing",
147 // customerDate = DateTime.Now
148 //};
149
150 //customerInfo.OrderInfoes.Add(new OrderInfo()
151 //{
152 // orderName = "car1",
153 //});
154
155 //customerInfo.OrderInfoes.Add(new OrderInfo()
156 //{
157 // orderName = "car2"
158 //});
159
160 //dbContext.Set<CustomerInfo>().Add(customerInfo);
161
162 //dbContext.SaveChanges();
163
164 #endregion
165
166 Console.WriteLine("OK");
167 Console.ReadKey();
168 }
169 }
170 }
参考文章:
ASP.NET EF(LINQ/Lambda查询):http://www.cnblogs.com/Dr-Hao/p/5356928.html
ASP.NET EF 使用LinqPad 快速学习Linq:http://www.cnblogs.com/Dr-Hao/p/5357112.html
Ps:ViewBag,ViewData,TempData区别?
ViewBag 和 ViewData 数据"共享",作用域为 当前 Action。
TempData 作用域跨 Action。
来源:https://www.cnblogs.com/DrHao/p/5255630.html
