ASP.NET Identity with EF Database First MVC5
Is it possible to use the new Asp.net Identity with Database First and EDMX? Or only with code first?
Here's what I did:
1) I made a new MVC5 Project and had the new Identity create the new User and Roles tables in my database.
2) I then opened my Database First EDMX file and dragged in the new Identity Users table since I have other tables that relate to it.
3) Upon saving the EDMX, the Database First POCO generator will auto create a User class. However, UserManager and RoleManager expects a User class inheriting from the new Identity namespace (Microsoft.AspNet.Identity.IUser), so using the POCO User class won't work.
I guess a possible solution is to edit my POCO Generation Classes to have my User class inherit from IUser?
Or is ASP.NET Identity only compatible with Code First Design?
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Update: Following Anders Abel's suggestion below, this is what I did. It work's, but I'm wondering if there is a more elegant solution.
1) I extended my entity User class by creating a partial class within the same namespace as my auto generated entities.
namespace MVC5.DBFirst.Entity
{
public partial class AspNetUser : IdentityUser
{
}
}
2) I changed my DataContext to inherit from IdentityDBContext instead of DBContext. Note that every time you update your EDMX and regenerate the DBContext and Entity classes, you'll have to set this back to this.
public partial class MVC5Test_DBEntities : IdentityDbContext<AspNetUser> //DbContext
3) Within your auto generated User entity class, you must add the override keyword to the following 4 fields or comment these fields out since they are inherited from IdentityUser (Step 1). Note that every time you update your EDMX and regenerate the DBContext and Entity classes, you'll have to set this back to this.
override public string Id { get; set; }
override public string UserName { get; set; }
override public string PasswordHash { get; set; }
override public string SecurityStamp { get; set; }
It should be possible to use the identity system with POCO and Database First, but you'll have to make a couple of tweaks:
partial
. That will make it possible for you to supply additional implementation in a separate file. User
class in another file partial User : IUser
{
}
That will make the User
class implement the right interface, without touching the actual generated files (editing generated files is always a bad idea).
My steps are very similar but I wanted to share.
1) Create a new MVC5 project
2) Create a new Model.edmx. Even if it's a new database and has no tables.
3) Edit web.config and replace this generated connectionstring:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)v11.0;AttachDbFilename=|DataDirectory|aspnet-SSFInventory-20140521115734.mdf;Initial Catalog=aspnet-SSFInventory-20140521115734;Integrated Security=True" providerName="System.Data.SqlClient" />
with this connectionstring:
<add name="DefaultConnection" connectionString="Data Source=.SQLExpress;database=SSFInventory;integrated security=true;" providerName="System.Data.SqlClient" />
Afterwards, build and run the application. Register a user and then the tables will be created.
EDIT: ASP.NET Identity with EF Database First for MVC5 CodePlex Project Template.
I wanted to use an existing database and create relationships with ApplicationUser. This is how I did it using SQL Server but the same idea would probably work with any DB.
:base("DefaltConnection")
to use your project's DbContext. Edit: Asp.Net Identity Class Diagram
链接地址: http://www.djcxy.com/p/90216.html上一篇: 从实体框架数据库优先切换到代码优先