joe*_*dev 8 c# asp.net visual-studio-2013
所以基本上在最后学习如何将OpenAuth改为不在.NET 4.5中使用DefaultConnection之后,我已经转到了4.5.1,让那些学习变得毫无意义.AuthConfig.cs的职责现在位于Startup.Auth.cs中,OpenAuth的静态方法已被抽象出来,因此我无法再直接更改OpenAuth.ConnectionString的默认值.
在.NET 4.5.1中更改成员身份的连接字符串/数据库的最佳做法是什么?
fer*_*adz 13
我遵循了你建议的方法,它对我有用.然而,几乎没有什么东西,语法和命名问题恰好是不同的.我认为这些差异可能是由于我们使用的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; }
}
Run Code Online (Sandbox Code Playgroud)
字段Roles,Users,UserClaims,UserLogins,UserRoles是因为提出要求的会员管理.但是,在我的情况下,他们的类型有不同的名称(ApplicationUser而不是User,IdentityUserClaim而不是UserClaim等).我想这就是为什么Antevirus有"无法找到用户"的问题.
另外,正如我们在案例中看到的那样,有5个这样的字段而不是8个.可能这是由于Visual Studio的不同版本.
我做的最后一次改变是在课堂上AccountController,它反映了新环境的使用MyDbContext.在这里,我传递了一个实例MyDbContext而不是ApplicationDbContext
之前
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
Run Code Online (Sandbox Code Playgroud)
后
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyDbContext())))
{
}
Run Code Online (Sandbox Code Playgroud)
适用于Microsoft.AspNet.Identity.EntityFramework 1.0.0-rc1
在AccountController的无参数构造函数中,更改该行
IdentityManager = new AuthenticationIdentityManager(new IdentityStore());
Run Code Online (Sandbox Code Playgroud)
至
IdentityManager = new AuthenticationIdentityManager(new IdentityStore(new DefaultIdentityDbContext("YourNameOrConnectionString")));
Run Code Online (Sandbox Code Playgroud)
而你很高兴.
适用于Microsoft.AspNet.Identity.EntityFramework 1.0.0
与我们为候选版本所做的类似,但我们在不同的地方执行此操作.打开IdentityModels.cs作为VS模板的一部分创建的并将以下构造函数添加到ApplicationDbContext类中:
public ApplicationDbContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
Run Code Online (Sandbox Code Playgroud)
你现在可以在改变参数的构造函数AccountController从
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
Run Code Online (Sandbox Code Playgroud)
至
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext("YourNameOrConnectionString"))))
{
}
Run Code Online (Sandbox Code Playgroud)
你做完了
| 归档时间: |
|
| 查看次数: |
12224 次 |
| 最近记录: |