亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

啟動時訪問 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 關注
  • 103 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號