简体   繁体   中英

How can I map one property to multiple relations with entity framework?

I am trying to use Entity framework 6.1 to build an application with a database first approach.

I am puzzled on how to build one-to-many relation while not using the default Key aka Id as the local property.

I have the following two models, first

public class UserToClient
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ForeignKey("ViewClient")]
    public int ClientId { get; set; }

    [ForeignKey("Team")]
    public int DefaultTeamId { get; set; }

    public bool IsActive { get; set; }

    public virtual ViewClient ViewClient { get; set; }
    public virtual Team Team { get; set; }

    public virtual ICollection<Team> Teams { get; set; }

}

Here is my second model

public class Team
{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public bool IsActive { get; set; }

    public string Name { get; set; }

    [ForeignKey("ViewClient")]
    public int ClientId { get; set; }

    public virtual ICollection<ViewClient> ViewClients { get; set; }

    public virtual UserToClient UserToClient { get; set; }

}

Within my UserToClient model, I want to create one-to-many relation "One UserToClient has many Team "

With the end results, I want to be able to do something like this

using(var connection = new AppContext())
{
   var userToClients = connection.UserToClients
                                 .Include(x => x.Team)
                                 .Include(x => x.Teams)
                                 .ToList();


     //Do something with userToClients

     //Do something with userToClients.Team

     //Do something with each team

     foreach(var team in userToClients.Teams)
     {
         //Do something with 'team'
     }
}

If I was to write a query manually "outside Entity" I will do something like this

SELECT *
FROM [UserToClient] AS r
LEFT JOIN [Team] AS t ON t.ClientId = r.ClientId

Problem

The relation needs to point at a property called ClientId not the primary key Id .

Entity is currently generating a query like this and I want to be able t change Id to ClientId .

SELECT *
FROM [UserToClient] AS r
LEFT JOIN [Team] AS t ON t.Id = r.ClientId

It is important to understand that Id is the primary key, but in this case I want to join at a key that isn't the primary one.

How can I fix this issue?

Attempt

I attempted to solve this problem by overriding the OnModelCreating method in the context class like this

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Team>()

                .HasRequired(x => x.UserToClient)
                .WithMany(x => x.Teams)
                .HasForeignKey(f => f.ClientId);

}

I need a way to tell the relation that the local propery is ClientId and not Id

How can I fix it?

I believe for this situation you need to also use the Fluent API to designate this type of relationship. A similar question with the answer is available here

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