Entity Framework 最佳實踐

来源:互联网 发布:centos 7 syslog 编辑:程序博客网 时间:2024/04/30 11:05

Extensions

ObjectContextExtensions.cs

主要是Cascading Delete

using System;using System.Collections;using System.Collections.Generic;using System.Data.Objects;using System.Data.Objects.DataClasses;using System.Linq;using System.Reflection;namespace Ezfx.EntityFrameworkExtensions{    public static class ObjectContextExtensions    {        public static void CascadingDeleteOnSubmit(this ObjectContext context, object entity)        {            List<object> objects2Delete = new List<object>();            CascadingDeleteOnSubmit(context, entity, objects2Delete);            foreach (object o in objects2Delete)            {                context.DeleteObject(o);            }        }        public static void CascadingDeleteOnSubmit(this ObjectContext context, object entity, List<object> object2Delete)        {            Type entityType = entity.GetType();            PropertyInfo[] entityProperties = entityType.GetProperties();            //查找是否有“AssociationAttribute”标记的属性            //(Linq中有“AssociationAttribute”标记的属性代表外表)            IEnumerable<PropertyInfo> associationProperties = entityProperties.Where(                 c => c.GetCustomAttributes(true).Any(                     attrbute => attrbute.GetType().Name == "EdmRelationshipNavigationPropertyAttribute")                     & c.PropertyType.IsGenericType);//该属性必需是泛型            //其他表有外键关联的记录            foreach (PropertyInfo associationProperty in associationProperties)            {                //获取Property值                object propertyValue = associationProperty.GetValue(entity, null);                if (propertyValue.GetType().GetGenericTypeDefinition() == typeof(EntityCollection<>))                {                    //Property是EntitySet`1类型的值,如EntitySet<Element>,                    //而EntitySet`1有IEnumerable接口                    IEnumerable enumerable = (IEnumerable)propertyValue;                    foreach (object o in enumerable)                    {                        //递归,删除关联的关联                        CascadingDeleteOnSubmit(context, o, object2Delete);                    }                }            }            try            {                //把要删除的关系放进object2Delete,                //这里没有直接删除,是因为直接删除会报错,foreach里的东西,不让删除                //这个函数的Linq版可以直接删除。                object2Delete.Add(entity);            }            catch (Exception ex)            {                throw ex;            }        }    }}


ObjectQueryExtensions.cs

Entity Frameword自帶了Include函數只能傳String型參數,以下函數可使用Func<>參數

using System;using System.Data.Objects;using System.Linq.Expressions;namespace Ezfx.EntityFrameworkExtensions{    public static class ObjectQueryExtensions    {        public static ObjectQuery<T> Include<T>(this ObjectQuery<T> mainQuery, Expression<Func<T, object>> subSelector)        {            return mainQuery.Include(((subSelector.Body as MemberExpression).Member as System.Reflection.PropertyInfo).Name);        }    }}


ObjectSetExtensions.cs

批量刪除,批量添加

using System.Collections.Generic;using System.Data.Objects;namespace Ezfx.EntityFrameworkExtensions{    public static class ObjectSetExtensions    {        public static void DeleteAllOnSubmit<TEntity>(this ObjectSet<TEntity> table, IEnumerable<TEntity> entities)            where TEntity : class, new()        {            foreach (TEntity entity in entities)            {                table.Detach(entity);            }        }        public static void InsertAllOnSubmit<TEntity>(this ObjectSet<TEntity> table, IEnumerable<TEntity> entities)            where TEntity : class, new()        {            foreach (TEntity entity in entities)            {                table.AddObject(entity);            }        }    }}


 

有很多人問,如何獲取數據庫表名,其實,他就在ObjectSet<T>.EntitySet.Name中。