启动时访问 dbcontext

使用 Identity 2 的 .NET MVC5 应用程序。

我需要在应用程序启动时调用我的服务的方法,但该方法需要访问数据库。我可以在哪里以及如何做到这一点?


qq_遁去的一_1
浏览 47回答 1
1回答

繁星coding

IdentityModel.cs是需要修改的地方// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.public class ApplicationUser : IdentityUser{&nbsp; &nbsp; public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType&nbsp; &nbsp; &nbsp; &nbsp; var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);&nbsp; &nbsp; &nbsp; &nbsp; // Add custom user claims here&nbsp; &nbsp; &nbsp; &nbsp; return userIdentity;&nbsp; &nbsp; }}public class ApplicationDbContext : IdentityDbContext<ApplicationUser>{&nbsp; &nbsp; public ApplicationDbContext()&nbsp; &nbsp; &nbsp; &nbsp; : base("DefaultConnection", throwIfV1Schema: false)&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public static ApplicationDbContext Create()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return new ApplicationDbContext();&nbsp; &nbsp; }}您可以创建多个环境变量,而不是默认连接。希望这能回答您的问题
打开App,查看更多内容
随时随地看视频慕课网APP