更改ASP.NET身份以使用现有的数据库 [英] Change ASP.NET Identity to use existing database

查看:148
本文介绍了更改ASP.NET身份以使用现有的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC 5与数据库优先工作流。我已经在我现有的数据库中创建了身份表( AspNetUsers AspNetRoles 等),但是我遇到了问题使注册登录功能正常工作。



这是 IdentityModels.cs class

  public class ApplicationDbContext:IdentityDbContext< ApplicationUser> 
{
public ApplicationDbContext()
:base(MyConnectionString)//我使用MyConnectionString而不是DefaultConnection
{
}

这是来自 web.config 的EF连接字符串看起来像

 < connectionStrings>< add name =MyConnectionStringconnectionString =metadata = res:// * / Entities .MyModel.csdl | res://*/Entities.MyModel.ssdl | res://*/Entities.MyModel.msl; provider = System.Data.SqlClient; provider connection string =& quot; data source = My- Pc\SQLEXPRESS;初始目录= MyExistingDatabase;集成安全= True; MultipleActiveResultSets = True; App = EntityFramework& quot; providerName =System.Data.EntityClient/> 



从edmx生成的上下文类

  public partial class MyConnectionString:DbContext 
{
public MyConnectionString()
:base(name = MyConnectionString)
{
}

对我来说似乎都很好,应该可以在数据库中创建用户,但是我在登录或尝试分别注册时遇到以下错误:



登录:


实体类型ApplicationUser不是
当前上下文的模型的一部分



第73行:var result = await SignInManager.PasswordSignInAsync(model.Email,
model.Password,model.RememberMe,shouldLockout:false);



异常详细信息:System.InvalidOperationException:实体类型
ApplicationUser不是当前上下文的模型的一部分。 / p>

注册:


实体类型ApplicationUser不是
当前上下文的模型的一部分



行155:var result = await UserManager.CreateAsync(user,
model.Password );



异常详细信息:System.InvalidOperationException:实体类型
ApplicationUser不是当前上下文模型的一部分。


大多数文章似乎都专注于Code-First,以及如何从LocalDb到SQLExpress等以及如何更改连接字符串,但是我还没有能够很好地解决这个问题。



编辑,解决方案:由于@ChrFin提到,您可以并排使用它们这是我最后做的。我只是添加了一个新的 常规 连接字符串在 web.config 中,让它指向现有的数据库。 PS。请记住,连接字符串名称不能与现有连接字符串名称相同,并且必须与您向 ApplicationDbContext 构造函数提供的连接字符串名称相同。

解决方案

我认为ASP.NET Identity不支持这种情况,因为它需要一个 DbContext ,它扩展了 IdentityDbContext< ApplicationUser> (或类似的)。



为什么要强制身份进入你的DB-First上下文?

您可以使用身份识别代码与其他DB-First上下文无关...


I'm using ASP.NET MVC 5 with a Database-First workflow. I've created the Identity tables (AspNetUsers, AspNetRoles etc.) in my existing database however I'm having problems getting the register and login functionality to work properly.

This is the IdentityModels.cs class

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("MyConnectionString") // I use "MyConnectionString" instead of "DefaultConnection" 
    {
    }

This is what the EF connection string from the web.config looks like

<connectionStrings><add name="MyConnectionString" connectionString="metadata=res://*/Entities.MyModel.csdl|res://*/Entities.MyModel.ssdl|res://*/Entities.MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=My-Pc\SQLEXPRESS;initial catalog=MyExistingDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

And for good measure here's the context class generated from the edmx

public partial class MyConnectionString : DbContext
{
    public MyConnectionString()
        : base("name=MyConnectionString")
    {
    }

To me all seems well and it should be able to create users in the database however I'm getting the following error when logging in or trying to register respectively:

For login:

The entity type ApplicationUser is not part of the model for the current context

Line 73: var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

Exception Details: System.InvalidOperationException: The entity type ApplicationUser is not part of the model for the current context.

For register:

The entity type ApplicationUser is not part of the model for the current context

Line 155: var result = await UserManager.CreateAsync(user, model.Password);

Exception Details: System.InvalidOperationException: The entity type ApplicationUser is not part of the model for the current context.

Most articles around seems to be focusing on Code-First and how to go from LocalDb to SQLExpress etc. and how to change the connection string however I haven't been able to solve this problem for a good amount of time.

Edit, solution: As @ChrFin mentioned you could use them both side by side which is what I ended up doing. I simply added a new regular connection string in web.config and let it point to the existing database. PS. remember that the connection string name cannot be the same as the existing one and must be the same you provide to the ApplicationDbContext constructor.

解决方案

I THINK this scenario is not supported by ASP.NET Identity as it needs a DbContext which extends IdentityDbContext<ApplicationUser> (or similar).

Why are you forcing Identity into your DB-First context?
You can use a Code-First context for Identity alongside your other DB-First context without problems...

这篇关于更改ASP.NET身份以使用现有的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆