簡體   English   中英

在.NET 4.5.1中設置ASP.NET Identity ConnectionString屬性

[英]Set ASP.NET Identity ConnectionString property in .NET 4.5.1

所以基本上在最后學習如何將OpenAuth改為不在.NET 4.5中使用DefaultConnection之后 ,我已經轉到了4.5.1,讓那些學習變得毫無意義。 AuthConfig.cs的職責現在位於Startup.Auth.cs中,OpenAuth的靜態方法已被抽象出來,因此我無法再直接更改OpenAuth.ConnectionString的默認值。

在.NET 4.5.1中更改成員身份的連接字符串/數據庫的最佳做法是什么?

我遵循了你建議的方法,它對我有用。 然而,幾乎沒有什么東西,語法和命名問題恰好是不同的。 我認為這些差異可能是由於我們使用的Visual Studio的不同版本(而不是.NET - 我的版本是.NET 4.5.1的版本)。 我繼續描述我的具體解決方案。

我的目標是擁有一個數據庫上下文,我可以使用該上下文訪問用戶或身份相關數據以及我的自定義應用程序數據。 為了實現這一點,我完全刪除了在創建新項目時自動為您創建的ApplicationDbContext類。

然后,我創建了一個新類MyDbContext

public class MyDbContext: DbContext
{
    public MyDbContext() : base("name=DefaultConnection")
    {

    }

    //
    // These are required for the integrated user membership.
    //
    public virtual DbSet<IdentityRole> Roles { get; set; }
    public virtual DbSet<ApplicationUser> Users { get; set; }
    public virtual DbSet<IdentityUserClaim> UserClaims { get; set; }
    public virtual DbSet<IdentityUserLogin> UserLogins { get; set; }
    public virtual DbSet<IdentityUserRole> UserRoles { get; set; }

    public DbSet<Movie> Movies { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<Purchase> Purchases { get; set; }
}

RolesUsersUserClaimsUserLoginsUserRoles字段是成員資格管理所需的建議。 但是,在我的情況下,他們的類型有不同的名稱( ApplicationUser而不是UserIdentityUserClaim而不是UserClaim等)。 我想這就是為什么Antevirus有“無法找到用戶”的問題。

另外,正如我們在案例中看到的那樣,有5個這樣的字段而不是8個。可能這是由於Visual Studio的不同版本。

我做的最后一個更改是在AccountController類中,它反映了新上下文MyDbContext 在這里,我傳遞了一個MyDbContext實例,而不是ApplicationDbContext

之前

public AccountController()
    : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}

public AccountController()
    : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyDbContext())))
{
}

發布候選

適用於Microsoft.AspNet.Identity.EntityFramework 1.0.0-rc1

在AccountController的無參數構造函數中,更改該行

IdentityManager = new AuthenticationIdentityManager(new IdentityStore());

IdentityManager = new AuthenticationIdentityManager(new IdentityStore(new DefaultIdentityDbContext("YourNameOrConnectionString")));

而你很高興。

發布

適用於Microsoft.AspNet.Identity.EntityFramework 1.0.0

與我們為候選版本所做的類似,但我們在不同的地方執行此操作。 打開作為VS模板的一部分創建的IdentityModels.cs ,並將以下構造函數添加到ApplicationDbContext類:

public ApplicationDbContext(string nameOrConnectionString)
    : base(nameOrConnectionString)
{
}

現在,您可以更改AccountController的無參數構造函數

public AccountController()
    : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}

public AccountController()
    : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext("YourNameOrConnectionString"))))
{
}

你做完了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM