Entity Framework 最佳實踐

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中。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

织网者Eric

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值