简体   繁体   中英

In an Asp.net project using identity v2 and Entity Framework how can I move all my code first models and User model identity to a new project

I have a web API 2 project and I use Entity Framework code-first models which has some foreign keys to identity version 2 users table that is bound to webapi 2 project. Since I have another provider project I have a reference to provider in my service (web api 2) project and since provider needs a reference to models putting models in service project cause circular reference.

Long story short I need to be able to move the code first models to a new project called Models to be used in both project. The problem is that no matter how hard I tried I couldn't move the IdentityUser model to a separate project since it needs reference to Microsoft.AspNet.Identity.EntityFramework

Below is an example of one of the models I use and ApplicationUser derives from IdentityUser :

public class Application
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Owner { get; set; }
    public string Company { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PaymentInfo { get; set; }
    public DateTime CreateDate { get; set; }
    public DateTime ExpireDate { get; set; }

    public virtual ApplicationUser User { get; set; }
    public virtual ICollection<Billing> Billings { get; set; }
}

I had the same issue few months ago.

ApplicationUser has to be inherited from IdentityUser. And there is no other way to keep all models of your project in one class library project without using required references when you using Identity framework.

My suggestion is to use separate project (class library) to handle all the identity stuff rather than keeping it on your web API 2 project. Then you can reference that project anywhere when you needed. This will avoid having Web Api 2 project referencing on Models project.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM