0

I have an MVC 5 project where I have separated out my models into a deparate project, say Project.Models. The Identity (ApplicationUser) model and its DbContext are still in the main project, as appears in the default MVC 5 project scaffold. One of the entities in the Project.Models assembly must have a property that is a list of ApplicationUsers.

Now I am starting to think that separating out the entity models into a separate project was not a good idea, because now I have the entity models and the Idenity models in separate contexts and assemblies.

How can I make a list of ApplicaitonUsers be part of an entity object if these are in separate assemblies? Should I merge the projects back into one assembly and DbContexgt or is there an easy way to solve this? I cannot define a DbSet<MainProject.ApplicationUser> in DbContext of Project.Models because this would mean circular reference.

Thanks.

3
  • It is a good convention to pull your data layer out into another project for portability purposes as well as many other adavantages. What was your reasoning with leaving the user identity models out of this abstraction? They share the same data source as your other domain models correct? Commented Aug 25, 2015 at 13:52
  • The reason is that the Identity model is tightly integrated with the HTTP and other web stuff, like setting auth cookies, etc, therefore it needs all these references which a pure POCO models won't need... Although I must admit that I still do not fully understand how deep that integration goes, I might be wrong there. Commented Aug 25, 2015 at 14:00
  • 1
    You are correct, identity is tightly coupled and a pain to separate out. One way around it is to create an ApplicationUser class (copy) in your models and do a DbSet<ApplicationUser> in your DbContext. Then you can add references in your models. Works for us. Commented Aug 25, 2015 at 14:59

1 Answer 1

1

You can add Identity related packages Microsoft.AspNet.Identity and Microsoft.AspNet.Identity.EntityFramework in your Models project and move ApplicationUser class to models project.

You can also add profile data in ApplicationUser like this:

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, string> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        // Add custom user claims here

        return userIdentity;
    }
}

Also, to make it a single DBContext, you can directly inherit from IdentityDbContext in your dbcontext class. Like this:

public partial class MyDBContext : IdentityDbContext<ApplicationUser>
{
    public MyDBContext()
        : base("name=DefaultConnection", throwIfV1Schema: false)
    {
    }
}

This way you wouldn't need to define DbSet<MainProject.ApplicationUser> in your context class.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for highlighting this method, but I wanted the Models assembly to be completely free of web-related stuff. Thanks
It looks like this is impossible to do in a completely clean way - some compromises need to be made, either by duplicating a class def or by dragging web references into a models-only assembly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.