EntityFramework用法探索(一)DatabaseFirst

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用法探索系列

完整代码下载

 





本文转自匠心十年博客园博客,原文链接:http://www.cnblogs.com/gaochundong/archive/2013/06/06/entityframework_usage_database_first.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值