我已经工作了好几天让Identity Framework在我的数据库第一个项目上工作。我出版的这本书只是解释了如何首先使用数据库来实现它,但它提到了OWIN。
我可以从代码第一版看到,我们可以通过在数据库上下文中调用静态Create
方法来获取数据库上下文的OWIN实例,该方法返回自身的实例。
不是OWIN与数据库第一模型和数据库上下文兼容吗?如果是这样,我如何使用OWIN获取我的数据库上下文的实例?
首先在代码中执行此操作:
数据库上下文
public class AppIdentityDbContext : IdentityDbContext<AppUser> {
public AppIdentityDbContext() : base("IdentityDb") {}
public static Create() {
return new AppIdentityDbContext();
}
}
从OWIN开始上课:
app.CreatePerOwinContext<AppIdentityDbContext>(AppIdentityDbContext.Create);
答案 0 :(得分:2)
我意识到OWIN的东西与您的应用程序首先使用实体框架代码还是首先使用数据库直接相关。
OWIN上下文用于访问ASP.NET Identity Framework的UserManager,RoleManager等。在此上下文中指定的DbContext实际上仅供这些类使用,而不仅仅用于ASP.NET MVC应用程序中的一般用途。
我现在正在使用IoC Container将DbContext注入我项目中的控制器。 (我使用Ninject,但还有其他如Unity,MEF)