标签:
EF(EntityFrameWork) ORM(对象关系映射框架/数据持久化框架),根据实体对象操作数据表中数据的一种面向对象的操作框架,底层也是调用ADO.NET
ASP.NET MVC 项目会自动导入MVC程序集,因为默认.NET环境(GAC)中没有这个程序集
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
1:LINQ/Lambda 基本查询
DbContext context = new MyFirstEFEntities(); // LINQ 基本查询 var rows = from c in context.Set<CustomerInfo>() select c; //返回的是SQL文 Console.WriteLine(rows); Console.WriteLine(rows.Count()); //Lambda 基本查询 var rows1 = context.Set<CustomerInfo>().Select(c => c); Console.WriteLine(rows1); Console.WriteLine(rows1.Count());
对应SQL为:
--基本查询 SELECT [Extent1].[id] AS [id], [Extent1].[customerName] AS [customerName], [Extent1].[customerDate] AS [customerDate] FROM [dbo].[CustomerInfo] AS [Extent1]
2:LINQ/Lambda 单条件查询
DbContext context = new MyFirstEFEntities(); //LINQ 单条件查询 var rows = from c in context.Set<CustomerInfo>() where c.id > 2 select c; Console.WriteLine(rows); Console.WriteLine(rows.Count()); //Lambda 单条件查询 var rows1 = context.Set<CustomerInfo>().Where(c => (c.id > 2)); Console.WriteLine(rows1); Console.WriteLine(rows1.Count());
对应SQL为:
--单条件查询 SELECT [Extent1].[id] AS [id], [Extent1].[customerName] AS [customerName], [Extent1].[customerDate] AS [customerDate] FROM [dbo].[CustomerInfo] AS [Extent1] WHERE [Extent1].[id] > 2
3:LINQ/Lambda 多条件查询
DbContext context = new MyFirstEFEntities(); //LINQ 多条件查询 var rows = from c in context.Set<CustomerInfo>() where c.id > 2 && c.customerName.Contains("today") select c; Console.WriteLine(rows); Console.WriteLine(rows.Count()); //Lambda 多条件查询 var rows1 = context.Set<CustomerInfo>().Where(c => (c.id > 2) && (c.customerName.Contains("today"))); Console.WriteLine(rows1); Console.WriteLine(rows1.Count());
对应SQL为:
--多条件查询 SELECT [Extent1].[id] AS [id], [Extent1].[customerName] AS [customerName], [Extent1].[customerDate] AS [customerDate] FROM [dbo].[CustomerInfo] AS [Extent1] WHERE ([Extent1].[id] > 2) AND ([Extent1].[customerName] LIKE N‘%today%‘)
4:LINQ/Lambda 连接查询
DbContext context = new MyFirstEFEntities(); //LINQ 连接查询 var rows = from c in context.Set<CustomerInfo>() join o in context.Set<OrderInfo>() on c.id equals o.customerId select c; //连接查询相当于是Inner join Console.WriteLine(rows); Console.WriteLine(rows.Count()); //Lambda 连接查询 var rows1 = context.Set<CustomerInfo>().Join(context.Set<OrderInfo>(), c => c.id, o => o.customerId, (c, o) => c); Console.WriteLine(rows1); Console.WriteLine(rows1.Count());
对应SQL为:
--连接查询 SELECT [Extent1].[id] AS [id], [Extent1].[customerName] AS [customerName], [Extent1].[customerDate] AS [customerDate] FROM [dbo].[CustomerInfo] AS [Extent1] INNER JOIN [dbo].[OrderInfo] AS [Extent2] ON [Extent1].[id] = [Extent2].[customerId]
5:LINQ:多from查询:专用于有导航属性的查询(LINQ特有)
DbContext context = new MyFirstEFEntities(); //多from查询:专用于有导航属性的查询(LINQ特有) //select CustomerInfo var rows = from c in context.Set<CustomerInfo>() from o in c.OrderInfoes where c.id > 2 && o.orderName.Contains("car") select c; Console.WriteLine(rows); Console.WriteLine(rows.Count()); //select OrderInfo var rows1 = from c in context.Set<CustomerInfo>() from o in c.OrderInfoes where c.id > 2 && o.orderName.Contains("car") select o; Console.WriteLine(rows1); Console.WriteLine(rows1.Count());
对应SQL为:
--多from查询:专用于有导航属性的查询(LINQ特有) --select CustomerInfo SELECT [Extent1].[id] AS [id], [Extent1].[customerName] AS [customerName], [Extent1].[customerDate] AS [customerDate] FROM [dbo].[CustomerInfo] AS [Extent1] INNER JOIN [dbo].[OrderInfo] AS [Extent2] ON [Extent1].[id] = [Extent2].[customerId] WHERE ([Extent1].[id] > 2) AND ([Extent2].[orderName] LIKE N‘%car%‘) --select OrderInfo SELECT [Extent1].[id] AS [id], [Extent1].[orderName] AS [orderName], [Extent1].[customerId] AS [customerId] FROM [dbo].[OrderInfo] AS [Extent1] WHERE ([Extent1].[customerId] IS NOT NULL) AND ([Extent1].[customerId] > 2) AND ([Extent1].[orderName] LIKE N‘%car%‘)
6:LINQ/Lambda 查询部分列(指定列)
DbContext context = new MyFirstEFEntities(); //LINQ 查询部分列(指定列) var rows = from c in context.Set<CustomerInfo>() select new { Id = c.id, Name = c.customerName }; Console.WriteLine(rows); foreach (var row in rows) { Console.WriteLine(row.Id + ":" + row.Name); } //Lambda 查询部分列(指定列) var rows1 = context.Set<CustomerInfo>().Select(c => (new { Id = c.id, Name = c.customerName })); foreach (var row in rows1) { Console.WriteLine(row.Id + ":" + row.Name); }
对应SQL为:
--查询部分列(指定列) SELECT [Extent1].[id] AS [id], [Extent1].[customerName] AS [customerName] FROM [dbo].[CustomerInfo] AS [Extent1]
7:LINQ/Lambda 查询多个表格部分列(指定列)
DbContext context = new MyFirstEFEntities(); //LINQ 查询多个表格部分列 var rows = from c in context.Set<CustomerInfo>() join o in context.Set<OrderInfo>() on c.id equals o.customerId select new { CustomerName = c.customerName, OrderName = o.orderName }; Console.WriteLine(rows); Console.WriteLine(rows.Count()); var rows1 = context.Set<CustomerInfo>().Join(context.Set<OrderInfo>(), c => c.id, o => o.customerId, (c, o) => (new { CustomerName = c.customerName, OrderName = o.orderName })); Console.WriteLine(rows1); Console.WriteLine(rows1.Count());
对应SQL为:
--查询多个表格部分列(指定列) SELECT [Extent1].[id] AS [id], [Extent1].[customerName] AS [customerName], [Extent2].[orderName] AS [orderName] FROM [dbo].[CustomerInfo] AS [Extent1] INNER JOIN [dbo].[OrderInfo] AS [Extent2] ON [Extent1].[id] = [Extent2].[customerId]
8:Lambda:分页查询 lambda特有 OrderBy/Skip/Take
DbContext context = new MyFirstEFEntities(); int pageSize = 2; int pageIndex = 2; //需要构建出startIndex 和 EndedIndex var rows = context.Set<CustomerInfo>().OrderBy(c => c.id).Skip(pageSize * (pageIndex - 1)).Take(2); Console.WriteLine(rows); Console.WriteLine(rows.Count());
对应SQL为:
--分页查询 lambda特有 SELECT TOP (2) [Extent1].[id] AS [id], [Extent1].[customerName] AS [customerName], [Extent1].[customerDate] AS [customerDate] FROM ( SELECT [Extent1].[id] AS [id], [Extent1].[customerName] AS [customerName], [Extent1].[customerDate] AS [customerDate], row_number() OVER (ORDER BY [Extent1].[id] ASC) AS [row_number] FROM [dbo].[CustomerInfo] AS [Extent1] ) AS [Extent1] WHERE [Extent1].[row_number] > 2 ORDER BY [Extent1].[id] ASC
整体EF 查询Demo源代码:
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 #region 查询LINQ Lambda表达式 EF 167 //DbContext context = new MyFirstEFEntities(); 168 //// LINQ 基本查询 169 //var rows = from c in context.Set<CustomerInfo>() 170 // select c; 171 ////返回的是SQL文 172 //Console.WriteLine(rows); 173 //Console.WriteLine(rows.Count()); 174 175 ////Lambda 基本查询 176 //var rows1 = context.Set<CustomerInfo>().Select(c => c); 177 //Console.WriteLine(rows1); 178 //Console.WriteLine(rows1.Count()); 179 180 ////LINQ 单条件查询 181 //var rows = from c in context.Set<CustomerInfo>() 182 // where c.id > 2 183 // select c; 184 //Console.WriteLine(rows); 185 //Console.WriteLine(rows.Count()); 186 187 ////Lambda 单条件查询 188 //var rows1 = context.Set<CustomerInfo>().Where(c => (c.id > 2)); 189 //Console.WriteLine(rows1); 190 //Console.WriteLine(rows1.Count()); 191 192 ////LINQ 多条件查询 193 //var rows = from c in context.Set<CustomerInfo>() 194 // where c.id > 2 && c.customerName.Contains("today") 195 // select c; 196 //Console.WriteLine(rows); 197 //Console.WriteLine(rows.Count()); 198 199 ////Lambda 多条件查询 200 //var rows1 = context.Set<CustomerInfo>().Where(c => (c.id > 2) && (c.customerName.Contains("today"))); 201 //Console.WriteLine(rows1); 202 //Console.WriteLine(rows1.Count()); 203 204 ////LINQ 连接查询 205 //var rows = from c in context.Set<CustomerInfo>() 206 // join o in context.Set<OrderInfo>() on c.id equals o.customerId 207 // select c; 208 ////连接查询相当于是Inner join 209 //Console.WriteLine(rows); 210 //Console.WriteLine(rows.Count()); 211 212 ////Lambda 连接查询 213 //var rows1 = context.Set<CustomerInfo>().Join(context.Set<OrderInfo>(), c => c.id, o => o.customerId, (c, o) => c); 214 //Console.WriteLine(rows1); 215 //Console.WriteLine(rows1.Count()); 216 217 ////多from查询:专用于有导航属性的查询(LINQ特有) 218 ////select CustomerInfo 219 //var rows = from c in context.Set<CustomerInfo>() 220 // from o in c.OrderInfoes 221 // where c.id > 2 && o.orderName.Contains("car") 222 // select c; 223 //Console.WriteLine(rows); 224 //Console.WriteLine(rows.Count()); 225 226 ////select OrderInfo 227 //var rows1 = from c in context.Set<CustomerInfo>() 228 // from o in c.OrderInfoes 229 // where c.id > 2 && o.orderName.Contains("car") 230 // select o; 231 232 //Console.WriteLine(rows1); 233 //Console.WriteLine(rows1.Count()); 234 235 ////LINQ 查询部分列(指定列) 236 //var rows = from c in context.Set<CustomerInfo>() 237 // select new { Id = c.id, Name = c.customerName }; 238 //Console.WriteLine(rows); 239 //foreach (var row in rows) 240 //{ 241 // Console.WriteLine(row.Id + ":" + row.Name); 242 //} 243 244 ////Lambda 查询部分列(指定列) 245 //var rows1 = context.Set<CustomerInfo>().Select(c => (new { Id = c.id, Name = c.customerName })); 246 //foreach (var row in rows1) 247 //{ 248 // Console.WriteLine(row.Id + ":" + row.Name); 249 //} 250 251 ////LINQ 查询多个表格部分列 252 //var rows = from c in context.Set<CustomerInfo>() 253 // join o in context.Set<OrderInfo>() 254 // on c.id equals o.customerId 255 // select new { CustomerName = c.customerName, OrderName = o.orderName }; 256 //Console.WriteLine(rows); 257 //Console.WriteLine(rows.Count()); 258 259 //var rows1 = context.Set<CustomerInfo>().Join(context.Set<OrderInfo>(), c => c.id, o => o.customerId, (c, o) => (new { CustomerName = c.customerName, OrderName = o.orderName })); 260 //Console.WriteLine(rows1); 261 //Console.WriteLine(rows1.Count()); 262 263 ////分页查询 lambda特有 264 //int pageSize = 2; 265 //int pageIndex = 2; 266 ////需要构建出startIndex 和 EndedIndex 267 //var rows = context.Set<CustomerInfo>().OrderBy(c => c.id).Skip(pageSize * (pageIndex - 1)).Take(2); 268 //Console.WriteLine(rows); 269 //Console.WriteLine(rows.Count()); 270 #endregion 271 272 Console.WriteLine("OK"); 273 Console.ReadKey(); 274 } 275 } 276 }
参考链接:
ASP.NET MVC EF直接更新数据(不需查询):http://www.cnblogs.com/Dr-Hao/p/5255630.html
ASP.NET EF 使用LinqPad 快速学习Linq:http://www.cnblogs.com/Dr-Hao/p/5357112.html
标签:
原文地址:http://www.cnblogs.com/Dr-Hao/p/5356928.html