English 中文(简体)
ASP.NET 海关成员 提供和共享DbContext
原标题:ASP.NET custom MembershipProvider and shared DbContexts

我已经在 MVC 网络配置中创建了一个自定义的 Assession Provider 。 我的 GetUser 函数返回了我从标准 AssistUser 中继承的自定义的员工类别。 这样我就可以为每个用户提供更多细节, 如各种员工细节 。

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {   
        return new ModelRepository().GetModels<Employee>().Where(e => e.UserName == username).FirstOrDefault();
    }        

我面临的问题是,会员提供商产生了我存储器类的新实例(这创造了一个新的 DbContext ), 以检索雇员对象。 这个员工对象随后被传递到任何称为会员的要求/控制者行动。

Employee currentUser = (Employee)Membership.GetUser();

调用请求通常想要在记忆中创建一个新对象, 换句话说是一个新的疾病记录, 并指派先前检索到的用户到该记录中, 然后将它保存到 DB 的“ 坚固” / 坚固” 模式仓库中。 您可能可以看到这种情况的去向, 框架抱怨我试图将一个对象( 用户) 的上下文保存在它最初没有检索的上下文中 。

我目前相当黑客化的解决方案是使用从会员中检索到的用户的 ID。 GetUser 并去从我目前的模型存储库中重新检索雇员对象 。

newSickness.Employee = this.modelRepository.GetModelById<Employee>(this.me.Id.Value);

我试过拆解雇员的物品 但后来它失去了它懒惰的装满财产 我必须记住 试着把它再次附到我目前的仓库/文本上

我也读到过,让贵客户的会员提供商拥有与当前请求使用相同的背景是件好事。 任何如何实现这一点的想法,我如何确保会员提供商使用与用户执行控制器操作时生成的环境相同的背景?

问题回答

您可以为每个控制器设置一个单独的 DbContext 实例。

public class SomeController : Controller
{
    private DbContext context = new DbContext();
    private CustomMembershipProvider membershipProvider = new CutomMembershipProvider(this.context);

    ... Actions ....
}

据我所知,每个控制器有一个上下文是良好做法。

顺便说一句,如果你有几个仓库 尝试使用UnitOffork 与仓库模式。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签