Entity Framework学习三:查询、插入、更新和删除操作
1.LINQ過(guò)濾數(shù)據(jù)?
var query = from person in context.Peoplewhere person.FirstName.StartsWith("a")select person; var methodQuery = context.People.Where(p => p.FirstName.StartsWith("a"));
兩種不同的寫法,效果一樣。
- 多條件組合查找
var query = from person in context.Peoplewhere person.FirstName.StartsWith("a") &&person.LastName.EndsWith("b")select person; var methodQuery = context.People.Where(p => p.FirstName.StartsWith("a") && p.LastName.EndsWith("b"));
- 對(duì)查找結(jié)果排序升序:
var query = from person in context.Peoplewhere person.FirstName.StartsWith("a") &&person.LastName.EndsWith("b")orderby person.FirstName,person.LastNameselect person;var methodQuery = context.People.Where(p => p.FirstName.StartsWith("a") && p.LastName.EndsWith("b")).OrderBy(p => p.FirstName).ThenBy(p => p.LastName);
- 對(duì)查找結(jié)果排序降序:
var query = from person in context.Peoplewhere person.FirstName.StartsWith("a") &&person.LastName.EndsWith("b")orderby person.FirstName,person.LastNamedescendingselect person;var methodQuery = context.People.Where(p => p.FirstName.StartsWith("a") && p.LastName.EndsWith("b")).OrderByDescending(p => p.FirstName).ThenByDescending(p => p.LastName);
- 延遲加載和貪婪加載(Lazy Loading和eager loading)
如果不確定是否要加載關(guān)聯(lián)數(shù)據(jù)時(shí)就使用延遲加載,否則就使用貪婪加載。當(dāng)然這個(gè)不是固定的看具體什么場(chǎng)景下使用。默認(rèn)情況下延遲加載是生效的,可以在創(chuàng)建DbContex后配置選項(xiàng)
context.Configuration.LazyLoadingEnabled = false; 2.插入數(shù)據(jù)
多種方法,不多說(shuō)看代碼:
- 方法一
var person = new Person { BirthDate = new DateTime(1980, 1, 2), FirstName = "John", HeightInFeet = 6.1M, IsActive = true, LastName = "Doe", MiddleName = "M" }; person.Phones.Add(new Phone { PhoneNumber = "1-222-333-4444" }); person.Phones.Add(new Phone { PhoneNumber = "1-333-4444-5555" }); using (var context = new Context()) { context.People.Add(person); context.SaveChanges(); }
方法二:改變Entity的狀態(tài)
using (var context = new Context()) { context.Entry(person2).State = EntityState.Added; context.SaveChanges(); }
EntityState:Added、Deleted、Detached(DbContex不追蹤Entity狀態(tài))、Modified、Unchanged
3.更新數(shù)據(jù)
多種方法:
方法一:
using (var context = new Context()) { var person = context.People.Find(1); person.FirstName = "New Name"; context.SaveChanges(); }
方法二(建議使用)注意標(biāo)記部分:
var person2 = new Person { PersonId = 1, BirthDate = new DateTime(1980, 1, 2), FirstName = "Jonathan", HeightInFeet = 6.1m, IsActive = true, LastName = "Smith", MiddleName = "M" }; person2.Phones.Add(new Phone { PhoneNumber = "updated 1", PhoneId = 1, PersonId = 1 }); person2.Phones.Add(new Phone { PhoneNumber = "updated 2", PhoneId = 2, PersonId = 1 }); using (var context = new Context()) { context.Entry(person2).State = EntityState.Modified; context.SaveChanges(); }
你會(huì)發(fā)現(xiàn)執(zhí)行上面代碼只有Person數(shù)據(jù)更新了,Phone數(shù)據(jù)沒有更新,這是因?yàn)镮nsert和Update機(jī)制不一樣,Update時(shí)設(shè)置了Entity的EntityState,但是并沒有傳播到子數(shù)據(jù)的狀態(tài),需要都上面程序做些修改
using (var context = new Context()) { context.Entry(person2).State = EntityState.Modified; foreach (var phone in person2.Phones) { context.Entry(phone).State = EntityState.Modified; } context.SaveChanges(); }
在開發(fā)Web程序時(shí)可以使用AsNoTracking來(lái)提高查詢性能
using (var context = new Context()) { var query = context.People.Include(p => p.Phones).AsNoTracking(); foreach (var person in query) { foreach (var phone in person.Phones) { } } }
- 使用Attach 會(huì)改變狀態(tài)為Unchanged,并開始追蹤Entity的狀態(tài)
var person3 = new Person { PersonId = 1, BirthDate = new DateTime(1980, 1, 2), FirstName = "Jonathan", HeightInFeet = 6.1m, IsActive = true, LastName = "Smith", MiddleName = "M" }; using (var context = new Context()) { context.People.Attach(person3); person3.LastName = "Updated"; context.SaveChanges(); }
上面代碼執(zhí)行將僅僅只會(huì)更新LastName列。也可以用以下代碼替代Attach方法
context.Entry(person3).State = EntityState.Unchanged
4.刪除數(shù)據(jù)
- 方法一:先查詢出數(shù)據(jù),再刪除(注意刪除子數(shù)據(jù)可以使用設(shè)置數(shù)據(jù)庫(kù)級(jí)聯(lián)刪除更方便)
using (var context = new Context()) { var toDelete = context.People.Find(personId); toDelete.Phones.ToList().ForEach(phone =>context.Phones.Remove(phone)); context.People.Remove(toDelete); context.SaveChanges(); }
- 方法二:使用改變狀態(tài)
var toDeleteByState = new Person { PersonId = personId }; toDeleteByState.Phones.Add(new Phone { PhoneId = phoneId1, PersonId = personId }); toDeleteByState.Phones.Add(new Phone { PhoneId = phoneId2, PersonId = personId }); using (var context = new Context()) { context.People.Attach(toDeleteByState); foreach (var phone in toDeleteByState.Phones.ToList()){context.Entry(phone).State = EntityState.Deleted;}context.Entry(toDeleteByState).State = EntityState.Deleted;context.SaveChanges(); }
5.查詢本地?cái)?shù)據(jù)
當(dāng)需要查詢的數(shù)據(jù)已經(jīng)在內(nèi)存中,而未提交到數(shù)據(jù)庫(kù)時(shí)對(duì)內(nèi)存數(shù)據(jù)進(jìn)行查詢
var localQuery = context.People.Local.Where(p => p.LastName.Contains("o")).ToList();
轉(zhuǎn)載于:https://www.cnblogs.com/zjmsky/p/4823847.html
總結(jié)
以上是生活随笔為你收集整理的Entity Framework学习三:查询、插入、更新和删除操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 摩托车趴赛多少钱
- 下一篇: ROS知识(4)----初级教程之常见问