EntityFramework数据库优先方式,很明显,我们需要先设计数据库模型。
假设我们需要设计一个零售系统,需要一些表结构:
生成数据库建表SQL
View Code
生成数据库表
然后生成ADO.NET Entity Data Model,
选择数据库,
定义模型名空间,
得到数据模型edmx文件,
我们先定义DomainModels.Customer,
1 public class Customer 2 { 3 public long Id { get; set; } 4 public string Name { get; set; } 5 public string Address { get; set; } 6 public string Phone { get; set; } 7 8 public override string ToString() 9 { 10 return string.Format("Id[{0}], Name[{1}], Address[{2}], Phone[{3}]", 11 Id, Name, Address, Phone); 12 } 13 }
定义ICustomerRepository接口,
1 public interface ICustomerRepository 2 { 3 void InsertCustomer(Customer customer); 4 void UpdateCustomer(Customer customer); 5 List<Customer> GetAllCustomers(); 6 List<Customer> GetCustomersByAddress(string address); 7 void DeleteAllCustomers(); 8 void DeleteCustomersByAddress(string address); 9 }
开始定义增删改查操作,
新增:
1 public void InsertCustomer(DomainModels.Customer customer) 2 { 3 using (RetailEntities context = new RetailEntities()) 4 { 5 Customer entity = Mapper.Map<DomainModels.Customer, Customer>(customer); 6 context.Customers.AddObject(entity); 7 context.SaveChanges(); 8 9 customer.Id = entity.Id; 10 } 11 }
修改:
1 public void UpdateCustomer(DomainModels.Customer customer) 2 { 3 using (RetailEntities context = new RetailEntities()) 4 { 5 Customer entity = context.Customers.AsQueryable().Single(c => c.Id == customer.Id); 6 7 entity.Name = customer.Name; 8 entity.Address = customer.Address; 9 entity.Phone = customer.Phone; 10 11 context.SaveChanges(); 12 } 13 }
查询:
1 public List<DomainModels.Customer> GetCustomersByAddress(string address) 2 { 3 using (RetailEntities context = new RetailEntities()) 4 { 5 List<Customer> entities = context.Customers.AsQueryable().Where(c => c.Address == address).ToList(); 6 List<DomainModels.Customer> customers = new List<DomainModels.Customer>(); 7 8 foreach (var entity in entities) 9 { 10 DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity); 11 customers.Add(customer); 12 } 13 14 return customers; 15 } 16 }
删除:
1 public void DeleteCustomersByAddress(string address) 2 { 3 using (RetailEntities context = new RetailEntities()) 4 { 5 List<Customer> entities = context.Customers.AsQueryable().Where(c => c.Address == address).ToList(); 6 7 foreach (var entity in entities) 8 { 9 context.DeleteObject(entity); 10 } 11 12 context.SaveChanges(); 13 } 14 }
CustomerRepository完整实现:
View Code
具体使用:
1 ICustomerRepository customerRepository = new CustomerRepository(); 2 3 // =============== 增 =============== 4 Console.ForegroundColor = ConsoleColor.DarkRed; 5 6 DomainModels.Customer customer1 = new DomainModels.Customer() 7 { 8 Name = "Dennis Gao", 9 Address = "Beijing", 10 Phone = "18888888888", 11 }; 12 customerRepository.InsertCustomer(customer1); 13 Console.WriteLine(customer1);
完整代码和索引
EntityFramework用法探索系列
- (一)DatabaseFirst
- (二)CodeFirst
- (三)CodeFirst流畅API
- (四)Repository和UnitOfWork
- (五)引入Unity
- (六)静态Repository
- (七)线程安全实践
- (八)事务处理
本文转自匠心十年博客园博客,原文链接:http://www.cnblogs.com/gaochundong/archive/2013/06/06/entityframework_usage_database_first.html,如需转载请自行联系原作者