Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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; }
share|improve this question
    
do you have sample code of your implementation? as when i try to replicate the above, i get an error when i try to login or register a user."The entity type AspNetUser is not part of the model for the current context" where AspNetUser is my User entity –  Tim Nov 15 '13 at 10:53
    
Did you add the AspNetUser table to your EDMX? Also, make sure your AccountController is using MVC5Test_DBEntities (or whatever your DB context is named) rather than ApplicationContext. –  Patrick Tran Dec 4 '13 at 11:01
    
yes i did for both the above. –  Tim Dec 4 '13 at 14:57
    
Hi Patrick, i have been struggling with this and was wondering if you found a working solution? –  KevDevMan Feb 12 at 15:18
    
Glad to know I am NOT the only one with this problem. I too have resorted to a similar solution. Less than ideal...but it works. –  MrLane Mar 9 at 14:57
show 1 more comment

7 Answers

It should be possible to use the identity system with POCO and Database First, but you'll have to make a couple of tweaks:

  1. Update the .tt-file for POCO generation to make the entity classes partial. That will make it possible for you to supply additional implementation in a separate file.
  2. Make a partial implementation of the 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).

share|improve this answer
    
Thanks. I'll have to give this a try and report if it works out. –  Patrick Tran Nov 12 '13 at 21:59
    
I tried what you mentioned. See my post for updated info... but the solution wasn't very elegant :/ –  Patrick Tran Nov 13 '13 at 4:59
    
I've been having just the same problem. It seems that documentation on DB-first is very sparse. This is a nice suggestion but I think you're right, it doesn't quite work –  Phil Dec 6 '13 at 13:07
1  
Is there any solution as yet that is not a hack? –  user20358 Apr 8 at 13:13
add comment

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.

  1. Create an MVC Project
  2. Open the DB listed under the DefaultConnection in Web.config. It will be called (aspnet-[timestamp] or something like that.)
  3. Script the database tables.
  4. Insert the scripted tables into existing database in SQL Server Management Studio.
  5. Customize and add relationships to ApplicationUser (if necessary).
  6. Create new Web Project > MVC > DB First Project > Import DB with EF ... Excluding the Identity Classes you inserted.
  7. In IdentityModels.cs change the ApplicationDbContext :base("DefaltConnection") to use your project's DbContext.

Edit: Asp.Net Identity Class Diagram enter image description here

share|improve this answer
5  
The issue isn't DBContext, but that UserManager and RoleManager expect a class that inherits from Microsoft.AspNet.Identity.EntityFramework.IdentityUser –  Patrick Tran Nov 12 '13 at 21:56
    
public class IdentityDbContext<TUser> : DbContext where TUser : Microsoft.AspNet.Identity.EntityFramework.IdentityUser. When using database first, the generated entity classes do not inherit from any base classes. –  Patrick Tran Nov 12 '13 at 21:58
    
Then just exclude them from the database when you generate the classes with the entity framework. –  stink Nov 13 '13 at 5:23
    
If you exclude the Identity tables from the EDMX, then you will lose the Navigation properties in other classes that have foreign keys to your UserID –  Patrick Tran Nov 14 '13 at 22:53
13  
I'm not reluctant to move to code first... It's just that in some scenarios and companies, the db admin creates the base tables, not the coder. –  Patrick Tran Nov 16 '13 at 3:07
show 1 more comment

By no means do I think this is the most efficient solution, but this gets the groundwork paved for this approach.

IdentityUser is worthless here because it's the code-first object used by the UserStore for authentication. After defining my own User object, I implemented a partial class that implements IUser which is used by the UserManager class. I wanted my Ids to be int instead of string so I just return the UserID's toString(). Similarly I wanted n in Username to be uncapitalized.

public partial class User : IUser
{

    public string Id
    {
        get { return this.UserID.ToString(); }
    }

    public string UserName
    {
        get
        {
            return this.Username;
        }
        set
        {
            this.Username = value;
        }
    }
}

You by no means need IUser. It's only an interface used by the UserManager. So if you want to define a different "IUser" you would have to rewrite this class to use your own implementation.

public class UserManager<TUser> : IDisposable where TUser: IUser

You now write your own UserStore which handles all of the storage of users, claims, roles, etc. Implement the interfaces of everything that the code-first UserStore does and change where TUser : IdentityUser to where TUser : User where "User" is your entity object

public class MyUserStore<TUser> : IUserLoginStore<TUser>, IUserClaimStore<TUser>, IUserRoleStore<TUser>, IUserPasswordStore<TUser>, IUserSecurityStampStore<TUser>, IUserStore<TUser>, IDisposable where TUser : User
{
    private readonly MyAppEntities _context;
    public MyUserStore(MyAppEntities dbContext)
    { 
        _context = dbContext; 
    }

    //Interface definitions
}

Here are a couple examples on some of the interface implementations

async Task IUserStore<TUser>.CreateAsync(TUser user)
{
    user.CreatedDate = DateTime.Now;
    _context.Users.Add(user);
    await _context.SaveChangesAsync();
}

async Task IUserStore<TUser>.DeleteAsync(TUser user)
{
    _context.Users.Remove(user);
    await _context.SaveChangesAsync();
}

Using the MVC 5 template, I changed the AccountController to look like this.

public AccountController()
        : this(new UserManager<User>(new MyUserStore<User>(new MyAppEntities())))
{
}

Now logging in should work with your own tables. If anyone has ways that this could be improved. Add a comment and I will put it here.

share|improve this answer
add comment

Take a look at this project on GitHub: https://github.com/KriaSoft/AspNet.Identity

Which includes:

  • SQL Database Project Template for ASP.NET Identity 2.0
  • Entity Framework Database-First Provider(s)
  • Source Code and Samples

enter image description here

share|improve this answer
add comment

We have a Entity Model DLL project where we keep our model class. We also keep a Database Project with all of database scripts. My approach was as follows

1) Create your own project that has the EDMX using database first

2) Script the tables in your db, I used VS2013 connected to localDB (Data Connections) and copied the script to database project, add any custom columns, e.g. BirthDate [DATE] not null

3) Deploy the database

4) Update the Model (EDMX) project Add to the Model project

5) Add any custom columns to the application class

public class ApplicationUser : IdentityUser
{
    public DateTime BirthDate { get; set; }
}

In the MVC project AccountController added the following:

The Identity Provider want a SQL Connection String for it to work, to keep only 1 connection string for the database, extract the provider string from EF connection string

public AccountController()
{
   var connection = ConfigurationManager.ConnectionStrings["Entities"];
   var entityConnectionString = new EntityConnectionStringBuilder(connection.ConnectionString);
        UserManager =
            new UserManager<ApplicationUser>(
                new UserStore<ApplicationUser>(
                    new ApplicationDbContext(entityConnectionString.ProviderConnectionString)));
}
share|improve this answer
add comment

I spent several hours working through this and finally found a solution which I have shared on my blog here. Basically, you need to do everything said in stink's answer but with one additional thing: ensuring Identity Framework has a specific SQL-Client connection string on top of the Entity Framework connection string used for your application entities.

In summary, your application will use a connection string for Identity Framework and another for your application entities. Each connection string is of a different type. Read my blog post for a full tutorial.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.