移动ASP.NET识别模式类库 [英] Moving ASP.NET Identity model to class library

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

问题描述

我试图标识模型移动到使用此链接的方法的类库:

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

<一个href=\"http://www.umbraworks.net/bl0g/rebuildall/2013/10/22/Moving_ASP_NET_Identity_model_into_another_assembly\">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但在用户code没有处理

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

其他信息:发现与不变名称'System.Data.SqlClient的'ADO.NET提供程序没有实体框架提供。确保供应商在应用程序配置文件的EntityFramework部分注册。见<一href=\"http://go.microsoft.com/fwlink/?LinkId=260882\">http://go.microsoft.com/fwlink/?LinkId=260882了解更多信息。

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. 找到网站/型号/ 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" />
    


  • 从ApplicationDbContext让你的EF上下文类继承:

  • 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天全站免登陆