简体   繁体   中英

Recommended approach in MVC 5 to use Applicationuser as Navigation property in different DBContext

I am trying to separate the models in MVC 5 into a separate project called entities and have a different data access project.

  1. Project.Entities - domain models Project.Data - Data access layer with entity framework and repositories Project.Web - MVC Project

I have a class as shown below and would like to have it use the ApplicationUser Id as a foreign key

public class PoolMember
{
    public int PoolMemberId { get; set; }
    public int PoolId { get; set; }
    public int UserId { get; set; }        
    public AjoMemberStatus MemberStatus { get; set; }
    public DateTime DateJoined { get; set; }
    public Nullable<DateTime> DateStatusChanged { get; set; }
    public int PoolPayoutPosition { get; set; }

    public Pool Pool { get; set; }
    [ForeignKey("UserId")]
    public ApplicationUser ApplicationUser { get; set; }
}

It looks like my options are to move my entities into the same namespace as the ApplicationUser (which means adding it to the mvc project) or try to move the applicationuser out of the MVC project into the entities project.

My question is if anyone has any suggestions on a sustainable approach that allows the applicationuser class to be used as a foreign key in the models.

Your idea is correct, you should extract ApplicationUser class out of the default ASP.NET MVC project template and put it along with other business entities ( Project.Entities in your case).

Please have a look at this question for an example on structuring your ASP.NET MVC application.

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