.net通过MrAdvice实现AOP

免费的AOP
nuget安装MrAdvice;

using ArxOne.MrAdvice.Advice;
using System;
using System.Data.Entity;

namespace NBSharp.Core
{
    /// <summary>
    /// ef实现with nolock(备用,已用拦截器实现)
    /// 使用:在方法上加上[ReadUncommitedTransactionScope]即可
    /// </summary>
    [Serializable]
    public class ReadUncommitedTransactionScopeAttribute : Attribute//, IMethodAdvice
    {
        public void Advise(MethodAdviceContext context)
        {DbContext db = (DbContext)NBSharpResolver.Resolve<IUnitOfWork>();
            //第二种实现
            db.Database.ExecuteSqlCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");//with(nolock)

            context.Proceed();//执行aop标记的方法
//第二种实现
            //https://stackoverflow.com/questions/24684914/get-entity-framework-6-use-nolock-in-its-underneath-select-statements/24699606
            //db.Database.ExecuteSqlCommand("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;");//with(lock)
        }
    }
}

 

https://www.cnblogs.com/slyzly/articles/11388441.html

posted @ 2019-08-21 14:08  事理  阅读(547)  评论(0编辑  收藏  举报