为了账号安全,请及时绑定邮箱和手机立即绑定

启动时访问 dbcontext

启动时访问 dbcontext

C#
qq_遁去的一_1 2023-09-16 19:59:10
使用 Identity 2 的 .NET MVC5 应用程序。我需要在应用程序启动时调用我的服务的方法,但该方法需要访问数据库。我可以在哪里以及如何做到这一点?
查看完整描述

1 回答

?
繁星coding

TA贡献1797条经验 获得超4个赞

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

{

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)

    {

        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType

        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        // Add custom user claims here

        return userIdentity;

    }

}


public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

{

    public ApplicationDbContext()

        : base("DefaultConnection", throwIfV1Schema: false)

    {

    }


    public static ApplicationDbContext Create()

    {

        return new ApplicationDbContext();

    }

}

您可以创建多个环境变量,而不是默认连接。希望这能回答您的问题


查看完整回答
反对 回复 2023-09-16
  • 1 回答
  • 0 关注
  • 45 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信