在使用Entity Framework时,它很好的帮我们处理了数据处理层,在业务层,我们对实体的操作,也有一些通用的东西,比如对实体的增、删、改,这三种操作,大多数实体都相同,下面的代码就是通用的在业务处理层的方法实体,如果有单独的处理,子业务处理类可以继承这个业务父类来实现自己的功能。

         下面是定义业务父类,有三个公共方法:Add ,Remove,Mofify,和一个私有方法,GetEntitySetName,这个方作用是:在添加通用实体类时,将调用数据库实体对象的AddObject方汉,它的第一个参数需要一个实体集合名,这个名称可以通过GetEntitySetName方法得到,这个方法需要一个要添加的实体作为参数传递。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Data.Mapping;
using System.Data.Metadata.Edm;
using System.Data.EntityClient;
using System.Reflection;
using System.Collections;
 class BllEntity
    {
        //数据库对象
        protected TSMS_DBEntities tsms = new TSMS_DBEntities();
        /// <summary>
        /// 从映射文件中获取实体和数据表对应名称
        /// </summary>
        /// <param name="entityname">实体名</param>
        /// <returns>数据表名</returns>
        string GetEntitySetName(EntityObject entity)
        {
            Type entitytype = entity.GetType();//获取实体类型
            string entityname = entitytype.Name;//获取实体名称
 
            //构建连接对象
            EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder(System.Configuration.ConfigurationManager.ConnectionStrings["TSMS_DBEntities"].ConnectionString);
            string[] arrstr = ecsb.Metadata.Split('|');//得到映射文件
            EdmItemCollection e = new EdmItemCollection(arrstr[0]);//得到CSDL
            StoreItemCollection s = new StoreItemCollection(arrstr[1]);//得到SSDL
            StorageMappingItemCollection smt = new StorageMappingItemCollection(e, s, arrstr[2]);//得到MSL
            //获取合部实体对应关系
            var entities = smt[0].GetType().GetProperty("EntitySetMaps", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(smt[0], null);
            foreach (var entityvalue in (IList)entities)
            {
                //反射得到StorageSetMapping类型
                Assembly ass = Assembly.Load("System.Data.Entity,Version=3.5.0.0,culture=Neutral,PublicKeyToken=b77a5c561934e089");
                Type type = ass.GetType("System.Data.Mapping.StorageSetMapping");
                EntitySet es = (EntitySet)type.GetField("m_extent", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entityvalue);
                if (es.ElementType.Name == entityname)
                {
                    return es.Name;
                }
            }
            return null;
        }
        /// <summary>
        /// 添加实体对象
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Add(EntityObject entity)
        {
            try
            {
 
                string entitysetname = GetEntitySetName(entity);
                if (entitysetname != null)
                {
                    tsms.AddObject(entitysetname, entity);
                    tsms.SaveChanges();
                    return true;
                }
                else
                {
                    throw new BllException("获取数据库实体表失败!");
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Remove(EntityObject entity)
        {
            try
            {
                tsms.DeleteObject(tsms.GetObjectByKey(entity.EntityKey));
                tsms.SaveChanges();
                return true;
 
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public bool Modify(EntityObject entity)
        {
            try
            {
                object oldentity = tsms.GetObjectByKey(entity.EntityKey);
                EntityValueTransform.Transform((EntityObject)oldentity, entity);
                tsms.SaveChanges();
                return true;
 
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
因为查询的结果千奇百怪,所以没有把查询放到这个类中,个性的功能,放在子类中实现,如下:
class BllGrade : BllEntity
    {
        /// <summary>
        /// 获得成绩视图列表
        /// </summary>
        /// <returns>成绩实体列表</returns>
        public List<V_Grades> GetGrades()
        {
            return tsms.V_Grades.ToList();
        }
        /// <summary>
        /// 按编号获得成绩
        /// </summary>
        /// <param name="ID">唯一编号</param>
        /// <returns>成绩实体</returns>
        public Grade GetGradeByID(int ID)
        {
            return tsms.Grades.SingleOrDefault(grade => grade.ID == ID);
        }
    }