当前位置:编程学习 > asp >>

.NET基础篇——Entity Framework数据转换层通用类

在实现基础的三层开发的时候,大家时常会在数据层对每个实体进行CRUD的操作,其中存在相当多的重复代码。为了减少重复代码的出现,通常都会定义一个共用类,实现相似的操作,下面为大家介绍一下Entity Framework时常用到的通用类。
首先在数据库建立起几个关联表:Person、Company、Position,三个实体之间通过导航属性进行相互引用。
 \

下面为大家分别介绍以泛型实现的 Create、Read、Update、Delete 操作:
1. Create
在ObjectContext类之中,早已经为大家预定了一个Create 的操作 AddObject:
void ObjectContext.AddObject(entitySetName string,object entity)
void ObjectSet<T>.AddObject(T entity)
 
 1         public int Add<T>(T entity) where T : EntityObject
 2         {
 3             int changedCount = 0;
 4             using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
 5             {
 6                 try
 7                 {
 8                     using (BasicArchitectureEntities context = new BasicArchitectureEntities())
 9                     {
10                         context.AddObject(typeof(T).Name, entity);
11                         changedCount = context.SaveChanges();
12                         if (changedCount > 0)
13                             context.AcceptAllChanges();
14                         scope.Complete();
15                     }
16                 }
17                 catch (Exception ex)
18                 { ........ }
19             }
20             return changedCount;
21         }
 
从下面的测试可以看到,ObjectContext.AddObject(entitySetName string,object entity)已相当成熟,它不但可以加入单个实体,也可通过导航属性,一次性加入多个关联实体。
 
 1         static void Main(string[] args)
 2         {
 3             BaseCommand command = new BaseCommand();
 4             //建立关联实体
 5             Company company = new Company() { CompanyName = "Sun" ,Address="Beijing",Telephone="010-87654321"};
 6             Position position = new Position() { PositionName = "Project Manager", Salary = 15000.00, Company = company };
 7             //通过Add<T>同时加入实体对象company与position
 8             int n=command.Add<Position>(position);
 9
10             Console.ReadKey();
11         }
 
若要使用批量插入,只要在AddObject方法前多加一个重复语言即可,在此就不再多作解释了。
 
 1         public int AddList<T>(List<T> entityList) where T : EntityObject
 2         {
 3             int changedCount = 0;
 4             using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
 5             {
 6                 try
 7                 {
 8                     using (BasicArchitectureEntities context = new BasicArchitectureEntities())
 9                     {
10                         foreach (T entity in entityList)
11                             context.AddObject(typeof(T).Name, entity);
12                         changedCount = context.SaveChanges();
13                         if (changedCount > 0)
14                             context.AcceptAllChanges();
15                         scope.Complete();
16                     }
17                 }
18                 catch (Exception ex)
19                 { ....... }
20           

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,