将ASP.NET身份模型移动到类库 [英] Moving ASP.NET Identity model to class library

查看:80
本文介绍了将ASP.NET身份模型移动到类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此链接中的方法将Identity模型移到类库中:

I am trying to move the Identity model to a class library using the methods in this link:


服务库中的ASP.NET身份

问题1:似乎继续使用网站项目的连接字符串。我通过在类库中指定完整的连接字符串来克服它。我可以使IdentityDbContext使用类库的连接字符串吗?

Problem 1: It seems to keep using the Website project's connection string. I overcame it by specifying the full connection string in the class library. Can I make the IdentityDbContext use the class library's connection string?

问题2:由于问题1,如果我从网站项目中删除了实体框架。发生以下错误,它正在网站项目中寻找EF的SqlClient。

Problem 2: Due to the problem 1, if I remove the Entity Framework from the website project. It will give the following error that it is looking for EF's SqlClient in the Website project.


发生类型为System.InvalidOperationException的异常在EntityFramework.dll中,但未在用户代码中处理

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

其他信息:未找到具有不变名称System.Data.SqlClient的ADO.NET提供程序的实体框架提供程序。确保提供程序已注册在应用程序配置文件的entityFramework部分。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=260882 。 p>

Additional information: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

只要省略所有数据访问层参考,例如网站项目中的EF,欢迎其他解决方案。

Other solutions are welcome as long as it omits all Data Access Layer references like EF in the Website project.

推荐答案

将IdentityModel移动到类库中(根据 SRP ),请按照下列步骤操作:

To move the IdentityModel into a class library (which is the right thing to do according to the SRP), follow these steps:


  1. 创建一个类库。 (ClassLibrary1)

  2. 使用NuGet,添加对Microsoft.AspNet.Identity.EntityFramework的引用。

  3. 将您的网站中的参考添加到ClassLibrary1

  4. 查找WebSite / Models / IdentityModel.cs并移动将其添加到ClassLibrary1。

  5. 使IdentityModel.cs如下所示:

  1. Create a class library. (ClassLibrary1)
  2. Using NuGet, add a reference to Microsoft.AspNet.Identity.EntityFramework. This will also auto-add some other references.
  3. Add a reference in your website to ClassLibrary1
  4. Find WebSite/Models/IdentityModel.cs and move it to ClassLibrary1.
  5. Make IdentityModel.cs look like this:

public class ApplicationUser : IdentityUser
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("YourContextName")
    {
    }
}


  • 确保您的网站的Web.config中的YourContextName指向该部分中的正确数据库。 (注意:这个数据库可以并且应该存放你的应用程序数据)。

  • Make sure your website's Web.config has YourContextName pointing to the right database in the section. (Note: this database can and should house your application data).

    <add name="YourContextName" connectionString="YourConnectionStringGoesHere"
      providerName="System.Data.SqlClient" />
    


  • 使您的EF Context类继承自ApplicationDbContext:

  • Make your EF Context class inherit from your ApplicationDbContext:

    public class YourContextName : ApplicationDbContext
    {
        public DbSet<ABizClass1> BizClass1 { get; set; }
        public DbSet<ABizClass2> BizClass2 { get; set; }
        // And so forth ...
    }
    


  • 当您的站点中的任何人尝试登录或注册时,身份系统将使用所有数据将它们路由到您的数据库,其中包含身份表。

    When anyone in your site tries to log in or register, the Identity system will route them to your database with all your data which includes the Identity tables.

    很高兴!

    这篇关于将ASP.NET身份模型移动到类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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