首页 > 代码库 > Entity Framework + MySQL 使用笔记

Entity Framework + MySQL 使用笔记

添加:

 1 using (var edm = new NorthwindEntities()) 2  3 { 4  5 Customers c = new Customers { CustomerID = "c#", City = "成都市", Address = "中国四川省", CompanyName = "cnblogs", Country = "中国", Fax = "10086", Phone = "1008611", PostalCode = "610000", Region = "天府广场", ContactName = "风车车.Net" }; 6  7 edm.AddToCustomers(c); 8  9 int result = edm.SaveChanges();}

删除:

using (var edm = new NorthwindEntities()){Customers deletec = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c2");edm.DeleteObject(deletec);int result = edm.SaveChanges();}

修改:

1 using (var edm = new NorthwindEntities())2 {3 Customers addc = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c2");4 addc.City = "CD";5 addc.ContactName = "cnblogs";6 addc.Country = "CN";7 int result = edm.SaveChanges();8 }

分页+排序:

1 private static List<Post> GetPostList(int pageIndex, int pageSize)2 {3     int startRow = (pageIndex - 1) * pageSize;4     using (var db = new EdiBlogEntities())5     {6         var query = db.Post.OrderByDescending(p => p.PubDate).Skip(startRow).Take(pageSize);7         return query.ToList();8     }9 }

 

Entity Framework + MySQL 使用笔记