利用EF6简单实现多租户的应用(2)

public StoreContext() : base("Name=DefaultConnection") { //获取登录用户信息,tenantid var claimsidentity = (ClaimsIdentity)HttpContext.Current.User.Identity; var tenantclaim = claimsidentity?.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid"); var tenantid = Convert.ToInt32(tenantclaim?.Value); //设置当对Work对象进行查询时默认添加过滤条件 QueryFilterManager.Filter<Work>(q => q.Where(x => x.TenantId == tenantid)); //设置当对Order对象进行查询时默认添加过滤条件 QueryFilterManager.Filter<Order>(q => q.Where(x => x.TenantId == tenantid)); } public override Task<int> SaveChangesAsync(CancellationToken cancellationToken) { var currentDateTime = DateTime.Now; var claimsidentity = (ClaimsIdentity)HttpContext.Current.User.Identity; var tenantclaim = claimsidentity?.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid"); var tenantid = Convert.ToInt32(tenantclaim?.Value); foreach (var auditableEntity in this.ChangeTracker.Entries<Entity>()) { if (auditableEntity.State == EntityState.Added || auditableEntity.State == EntityState.Modified) { //auditableEntity.Entity.LastModifiedDate = currentDateTime; switch (auditableEntity.State) { case EntityState.Added: auditableEntity.Property("LastModifiedDate").IsModified = false; auditableEntity.Property("LastModifiedBy").IsModified = false; auditableEntity.Entity.CreatedDate = currentDateTime; auditableEntity.Entity.CreatedBy = claimsidentity.Name; auditableEntity.Entity.TenantId = tenantid; break; case EntityState.Modified: auditableEntity.Property("CreatedDate").IsModified = false; auditableEntity.Property("CreatedBy").IsModified = false; auditableEntity.Entity.LastModifiedDate = currentDateTime; auditableEntity.Entity.LastModifiedBy = claimsidentity.Name; auditableEntity.Entity.TenantId = tenantid; //if (auditableEntity.Property(p => p.Created).IsModified || auditableEntity.Property(p => p.CreatedBy).IsModified) //{ // throw new DbEntityValidationException(string.Format("Attempt to change created audit trails on a modified {0}", auditableEntity.Entity.GetType().FullName)); //} break; } } } return base.SaveChangesAsync(cancellationToken); } public override int SaveChanges() { var currentDateTime = DateTime.Now; var claimsidentity =(ClaimsIdentity)HttpContext.Current.User.Identity; var tenantclaim = claimsidentity?.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid"); var tenantid = Convert.ToInt32(tenantclaim?.Value); foreach (var auditableEntity in this.ChangeTracker.Entries<Entity>()) { if (auditableEntity.State == EntityState.Added || auditableEntity.State == EntityState.Modified) { auditableEntity.Entity.LastModifiedDate = currentDateTime; switch (auditableEntity.State) { case EntityState.Added: auditableEntity.Property("LastModifiedDate").IsModified = false; auditableEntity.Property("LastModifiedBy").IsModified = false; auditableEntity.Entity.CreatedDate = currentDateTime; auditableEntity.Entity.CreatedBy = claimsidentity.Name; auditableEntity.Entity.TenantId = tenantid; break; case EntityState.Modified: auditableEntity.Property("CreatedDate").IsModified = false; auditableEntity.Property("CreatedBy").IsModified = false; auditableEntity.Entity.LastModifiedDate = currentDateTime; auditableEntity.Entity.LastModifiedBy = claimsidentity.Name; auditableEntity.Entity.TenantId = tenantid; break; } } } return base.SaveChanges(); } DbContext.cs

经过以上3步就实现一个简单的多租户查询数据的功能。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdsypz.html