在.NET 4.5.1中设置ASP.NET Identity ConnectionString属性

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


So basically after finally learning how to change OpenAuth to not use DefaultConnection in .NET 4.5, I've moved on to 4.5.1, rendering those learnings moot. The duties of AuthConfig.cs now reside in Startup.Auth.cs, The static methods of OpenAuth have been abstracted away and therefore I can no longer change the default value of OpenAuth.ConnectionString directly.

所以基本上在最后学习如何将OpenAuth改为不在.NET 4.5中使用DefaultConnection之后,我已经转到了4.5.1,这些知识没有实际意义。 AuthConfig.cs的职责现在位于Startup.Auth.cs中,OpenAuth的静态方法已被抽象出来,因此我无法再直接更改OpenAuth.ConnectionString的默认值。

What is the best practice for changing Membership's connection string/database in .NET 4.5.1?

在.NET 4.5.1中更改成员身份的连接字符串/数据库的最佳做法是什么?

2 个解决方案

#1


13  

I have followed the approach you suggested and it worked for me. However, there were few things mostly syntactic and naming issues which happened to be different. I think these differences are due to probably the different versions of Visual Studios we used (rather than .NET - my version is release one with .NET 4.5.1). I continue with a description of my specific solution.

我遵循了你建议的方法,它对我有用。然而,几乎没有什么东西,语法和命名问题恰好是不同的。我认为这些差异可能是由于我们使用的Visual Studio的不同版本(而不是.NET - 我的版本是.NET 4.5.1的发布版本)。我继续描述我的具体解决方案。

My goal was to have a single DB context with which I can access both User or Identity related data and also my custom application data. To achieve this I completely deleted class ApplicationDbContext which is automatically created for you when you create a new project.

我的目标是拥有一个数据库上下文,我可以使用该上下文访问用户或身份相关数据以及我的自定义应用程序数据。为了实现这一点,我完全删除了在创建新项目时自动为您创建的ApplicationDbContext类。

Then, I created a new class MyDbContext.

然后,我创建了一个新类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; }
}

The fields Roles, Users, UserClaims, UserLogins, UserRoles are as suggested required for the membership management. However, in my case their types have different names (ApplicationUser instead of User, IdentityUserClaim instead of UserClaim and etc.). I suppose that was the reason why Antevirus had the "User could not be found" problem.

Roles,Users,UserClaims,UserLogins,UserRoles字段是成员资格管理所需的建议。但是,在我的情况下,他们的类型有不同的名称(ApplicationUser而不是User,IdentityUserClaim而不是UserClaim等)。我想这就是为什么Antevirus有“无法找到用户”的问题。

Also, as we see in my case there are 5 such fields rather than 8. Probably, this is due to the different versions of the Visual Studio.

另外,正如我们在案例中看到的那样,有5个这样的字段而不是8个。可能这是由于Visual Studio的不同版本。

The last change which I made was in class AccountController and it reflects the use of the new context MyDbContext. Here I passed an instance of MyDbContext instead of ApplicationDbContext

我做的最后一个更改是在AccountController类中,它反映了新上下文MyDbContext的使用。在这里,我传递了MyDbContext的实例,而不是ApplicationDbContext

Before

之前

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

After

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

#2


8  

Release Candidate

Works with Microsoft.AspNet.Identity.EntityFramework 1.0.0-rc1

适用于Microsoft.AspNet.Identity.EntityFramework 1.0.0-rc1

In the parameterless constructor for AccountController, change the line

在AccountController的无参数构造函数中,更改该行

IdentityManager = new AuthenticationIdentityManager(new IdentityStore());

to

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

and you're good to go.

你很高兴。

Release

Works with Microsoft.AspNet.Identity.EntityFramework 1.0.0

适用于Microsoft.AspNet.Identity.EntityFramework 1.0.0

Similar to what we did for release candidate, but we do this in a different place. Open IdentityModels.cs that was created as part of the VS template and add the following constructor to the ApplicationDbContext class:

与我们为候选版本所做的类似,但我们在不同的地方执行此操作。打开作为VS模板的一部分创建的IdentityModels.cs,并将以下构造函数添加到ApplicationDbContext类:

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

and you can now change the parameterless constructor in AccountController from

现在,您可以更改AccountController中的无参数构造函数

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

to

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

and your done.

你完成了。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/10/11/7dee18283e9c4fac25449857f5f6f5c9.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告