C# 将ASP.NET标识模型移动到类库

C# 将ASP.NET标识模型移动到类库,c#,asp.net,.net,entity-framework,asp.net-identity,C#,Asp.net,.net,Entity Framework,Asp.net Identity,我正在尝试使用此链接中的方法将标识模型移动到类库: 问题1:它似乎一直在使用网站项目的连接字符串。我通过在类库中指定完整的连接字符串来克服它。我可以让IdentityDbContext使用类库的连接字符串吗 问题2:由于问题1,如果我从网站项目中删除实体框架。它将给出以下错误,即它正在网站项目中查找EF的SqlClient EntityFramework.dll中发生“System.InvalidOperationException”类型的异常,但未在用户代码中处理 其他信息:未找到具有固定名

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

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

问题2:由于问题1,如果我从网站项目中删除实体框架。它将给出以下错误,即它正在网站项目中查找EF的SqlClient

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

其他信息:未找到具有固定名称“System.Data.SqlClient”的ADO.NET提供程序的实体框架提供程序。确保提供程序已在应用程序配置文件的“entityFramework”部分注册。有关更多信息,请参阅


其他解决方案是受欢迎的,只要它在网站项目中省略了所有数据访问层引用(如EF)。

将标识移动到类库中是什么意思?作为参考?我的自定义用户管理器和用户在一个单独的库中,但仅此而已

身份资料需要某种类型的数据存储。如果您将Identity配置为使用EF,请确保为其获取额外的nuget包,并且您应该能够在创建上下文时传递连接字符串

从我的头顶上

var mgr = new UserManager<ApplicationUser>(
     new IUserStore_ofYourChoice<ApplicationUser>(
       new DbContextName("ConnectionStringOverload"));
var-mgr=新用户管理器(
您选择的新IUserStore_(
新的DbContextName(“ConnectionStringOverload”);
我认为EF商店是“用户商店”,必须核实一下

现在有十几个不同的nuget数据存储包用于Identity。您不必使用EF,但它需要某种类型的存储

**编辑**

此外,作为参考,默认情况下,它将始终使用主项目的配置,以及它定义的连接字符串,这就是它的工作方式,因此这是确定的。

问题1:-

我想您可以使用以下链接中的解决方案:-

问题2:-

我相信实体框架的构建方式是在执行时使用该项目的web.config


要将IdentityModel移动到类库中(根据,这是正确的操作),请执行以下步骤:

  • 创建类库。(类库1)
  • 使用NuGet添加对Microsoft.AspNet.Identity.EntityFramework的引用。这也将自动添加一些其他引用
  • 在您的网站中将参考添加到ClassLibrary1
  • 找到网站/Models/IdentityModel.cs并将其移动到ClassLibrary1
  • 使IdentityModel.cs如下所示:

    public class ApplicationUser : IdentityUser
    {
    }
    
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("YourContextName")
        {
        }
    }
    
    公共类应用程序用户:IdentityUser
    {
    }
    公共类ApplicationDbContext:IdentityDbContext
    {
    公共应用程序上下文()
    :base(“YourContextName”)
    {
    }
    }
    
  • 确保您网站的Web.config中的YourContextName指向节中的正确数据库。(注意:此数据库可以也应该包含您的应用程序数据)

    
    
  • 使EF上下文类从ApplicationDbContext继承:

    public class YourContextName : ApplicationDbContext
    {
        public DbSet<ABizClass1> BizClass1 { get; set; }
        public DbSet<ABizClass2> BizClass2 { get; set; }
        // And so forth ...
    }
    
    public class YourContextName : ApplicationDbContext
    {
        public DbSet<ABizClass1> BizClass1 { get; set; }
        public DbSet<ABizClass2> BizClass2 { get; set; }
        // And so forth ...
    }
    
    public类YourContextName:ApplicationDbContext
    {
    公共DbSet BizClass1{get;set;}
    公共DbSet BizClass2{get;set;}
    //等等。。。
    }
    
  • 当您站点中的任何人尝试登录或注册时,标识系统会将他们与您的所有数据(包括标识表)一起路由到您的数据库


    干得好!

    有一些规则你必须知道并记住

    首先,Web项目将始终且仅使用Web.config文件它在自己的项目周期中找到的。无论您在解决方案的其他任何位置将什么放入任何其他配置文件中。web项目只能使用它在自己的项目中找到的内容。如果您在其他项目中有连接字符串,则必须将其复制到web项目中,否则将永远找不到它

    其次,假设web项目是您的启动项目,并且假设您正在使用数据迁移(因为Identity使用它),请注意,包管理器将始终使用它在启动项目中找到的连接字符串。因此,包管理器(用于更新数据库)不会在您的模型项目中使用连接字符串

    简单的解决办法是: 1.将连接字符串从模型项目复制到web项目。
    2.在package manager控制台中,确保用于选择上下文的下拉列表指向您的模型项目。

    更新@Rap的EF6和Identity 2.0答案:

  • 创建类库。(类库1)
  • 使用NuGet添加对Microsoft.AspNet.Identity.EntityFramework和Microsoft.AspNet.Identity.Owin的引用
  • 在您的网站中将参考添加到ClassLibrary1
  • 找到网站/Models/IdentityModel.cs并将其移动到ClassLibrary1
  • IdentityModel.cs应如下所示,无需更改任何内容:

    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
            // Add custom user claims here
            return userIdentity;
        }
    }
    
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }
    
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
    
    公共类应用程序用户:IdentityUser
    {
    公共异步任务GenerateUserIdentityAsync(UserManager管理器,字符串身份验证类型)
    {
    //注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
    var userIdentity=wait manager.CreateIdentityAsync(这是authenticationType);
    //在此处添加自定义用户声明
    返回用户身份;
    }
    }
    公共类ApplicationDbContext:IdentityDbContext
    {
    公共应用程序上下文()
    :base(“DefaultConnection”,throwifvv1schema:false)
    {
    }
    公共静态应用程序上下文创建()
    {
    返回新的ApplicationDbContext();
    }
    }
    
  • 确保网站的Web.config有一个指向节中正确数据库的上下文
    <connectionStrings>
      <add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=Project;Integrated Security=sspi;Pooling=false;" providerName="System.Data.SqlClient" />
    </connectionStrings>
    
    public class YourContextName : ApplicationDbContext
    {
        public DbSet<ABizClass1> BizClass1 { get; set; }
        public DbSet<ABizClass2> BizClass2 { get; set; }
        // And so forth ...
    }