Beispiel #1
0
        /// <summary>
        /// 全局查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        public virtual void SetGlobalQuery <T>(ModelBuilder builder) where T : EntityBase
        {
            var entityBuilder = builder.Entity <T>();//.HasQueryFilter(z => !z.Flag);

            //多租户
            //Console.WriteLine($"\t DbContext:{this.GetHashCode()} \tSetGlobalQuery<{typeof(T).Name}> this.EnableMultiTenant:" + this.EnableMultiTenant + $" / SiteConfig.SenparcCoreSetting.EnableMultiTenant:{SiteConfig.SenparcCoreSetting.EnableMultiTenant}");
            if (this.EnableMultiTenant && typeof(IMultiTenancy).IsAssignableFrom(typeof(T)) && !(typeof(IIgnoreMulitTenant).IsAssignableFrom(typeof(T))))
            {
                //多租户 + 软删除
                RequestTenantInfo requestTenantInfo = MultiTenantHelper.TryGetAndCheckRequestTenantInfo(ServiceProvider, $"SenparcEntitiesDbContextBase.SetGlobalQuery<{typeof(T).Name}>(ModelBuilder builder)", this);
                entityBuilder.HasQueryFilter(z => z.TenantId == requestTenantInfo.Id && !z.Flag);
            }
            else
            {
                //仅软删除
                entityBuilder.HasQueryFilter(z => !z.Flag);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 自动添加多租户Id
        /// </summary>
        private void AddTenandId()
        {
            if (this.EnableMultiTenant)
            {
                ChangeTracker.DetectChanges(); //
                var addedEntities = this.ChangeTracker
                                    .Entries()
                                    .Where(z => z.State == EntityState.Added)
                                    .Select(z => z.Entity)
                                    .ToList();

                RequestTenantInfo requestTenantInfo = null;
                foreach (var entity in addedEntities)
                {
                    if (!(entity is IIgnoreMulitTenant) && (entity is IMultiTenancy multiTenantEntity))
                    {
                        //如果未设置,则进行设定
                        requestTenantInfo          = requestTenantInfo ?? MultiTenantHelper.TryGetAndCheckRequestTenantInfo(ServiceProvider, "SenparcEntitiesDbContextBase.AddTenandId()", this);
                        multiTenantEntity.TenantId = requestTenantInfo.Id;
                    }
                }
            }
        }