第一篇 那种写法有些复杂,
简单办法是直接注入
切换到 ***.EntityFramework 项目
在Uow 里面创建 ***EfUnitOfWork.cs 类
public class CooperativeEfUnitOfWork : EfUnitOfWork { public CooperativeEfUnitOfWork(IIocResolver iocResolver, IConnectionStringResolver connectionStringResolver, IDbContextResolver dbContextResolver, IEfUnitOfWorkFilterExecuter filterExecuter, IUnitOfWorkDefaultOptions defaultOptions, IDbContextTypeMatcher dbContextTypeMatcher, IEfTransactionStrategy transactionStrategy) : base(iocResolver, connectionStringResolver, dbContextResolver, filterExecuter, defaultOptions, dbContextTypeMatcher, transactionStrategy) { } protected override void BeginUow() { base.BeginUow(); string major = AbpSession.GetMajor(); string projectCode = AbpSession.GetProjectCode(); SetFilterParameter(SysConsts.DataFilter.MajorFilter, SysConsts.DataFilter.MajorParameter, major); SetFilterParameter(SysConsts.DataFilter.ProjectCodeFilter, SysConsts.DataFilter.ProjectCodeParameter, projectCode); } }
然后在 **DataModule.cs 里 注册这个类
using Abp.Dependency; using Abp.Domain.Uow; using Abp.Modules; using Abp.Zero.EntityFramework; using JZFZ.Cooperative.EntityFramework; using JZFZ.Cooperative.Extensions; using JZFZ.Cooperative.Uow; using System.Data.Entity; using System.Reflection; using Abp.Domain.Repositories; using JZFZ.Cooperative.Authorization.Users; using JZFZ.Cooperative.EntityFramework.Repositories; namespace JZFZ.Cooperative { [DependsOn(typeof(AbpZeroEntityFrameworkModule), typeof(CooperativeCoreModule))] public class CooperativeDataModule : AbpModule { public override void PreInitialize() { Database.SetInitializer(new CreateDatabaseIfNotExists<CooperativeDbContext>()); Configuration.DefaultNameOrConnectionString = "Default"; Configuration.UnitOfWork.RegisterFilter(SysConsts.DataFilter.MajorFilter, true); Configuration.UnitOfWork.RegisterFilter(SysConsts.DataFilter.ProjectCodeFilter, true); Configuration.UnitOfWork.RegisterFilter(SysConsts.DataFilter.ActiveFilter, true); IocManager.Register<IUnitOfWork, CooperativeEfUnitOfWork>(DependencyLifeStyle.Transient); IocManager.Register<IRepository<User, long>, UserRepository>(DependencyLifeStyle.Transient); } public override void Initialize() { IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly()); } } }
注意标红的那部分代码