I am trying to move the Identity model to a class library using the methods in this link:
ASP.NET Identity in Services library
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?
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.
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
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.
Other solutions are welcome as long as it omits all Data Access Layer references like EF in the Website project.
To move the IdentityModel into a class library (which is the right thing to do according to the SRP), follow these steps:
Make IdentityModel.cs look like this:
public class ApplicationUser : IdentityUser { } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("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" />
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.
Good to go!
An update to @Rap's answer for EF6 and Identity 2.0:
IdentityModel.cs should look like this, no need to change anything:
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(); } }
Make sure your website's Web.config has a context pointing to the right database in the section. (Note: this database can and should house your application data).
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=Project;Integrated Security=sspi;Pooling=false;" providerName="System.Data.SqlClient" /> </connectionStrings>
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 ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With