我正在将一个应用从单租户迁移到多租户。在此过程中,我向所有必需的模型添加了tenantId,并更新了数据库(使用Entity Framework 5)。
然而,我需要更新所有需要更新的存储库。首先,我不知道如何在Model项目中获取当前用户id(然后是tenantId)(不依赖于WebSecurity,没有httpContext)。
其次,我不想在所有控制器中都写这种复杂/昂贵的代码。(获取UserId,进行数据库调用以获取tenantId,然后将其传递给Repository。)
public class PinTypeController : BaseController
{
private PinTypeRepository pinTypeRepo;
public PinTypeController()
{
UserRepository userRepo = new UserRepository();
UserProfile user = userRepo.GetById(WebSecurity.CurrentUserId);
this.pinTypeRepo = new PinTypeRepository(user.TenantId);
}
public ActionResult Index()
{
vmPinType vm = new vmPinType();
vm.NewPinType = new PinType();
vm.PinTypes = pinTypeRepo.GetAll();
return View(vm);
}
}
有没有更好的方法来存储tenantId,以便我可以从Model项目和所有仓库中访问它?