1

I need help. I have three static table (Dictionary of Country, Region and Cities). I set this tables in my Initilizer class. I need to create One to Many relation with my own User table without created Foregion Key in dictionaries tables by Fluent API.

Dictionary Tables (Countries, Regions and Cities):

enter image description here

My User table:

public class UserWorker
{
  public UserWorker()
    {
        Countries = new List<DCountry>();
        Regions = new List<DRegion>();
        Cities = new List<DCity>();
        Professions = new List<DProfession>();
    }

    public int UserWorkerId { get; set; }

    public virtual ICollection<DCountry> Countries { get; set; }

    public virtual ICollection<DRegion> Regions { get; set; }

    public virtual ICollection<DCity> Cities { get; set; }

    public virtual ICollection<DProfession> Professions { get; set; }
}

How I can add relation without UserWorker_UserWorkerId ?

P.S: I have another two my own table that need to connect by the same relation One to Many by Fluent API.

I don't understand how i can add Objects dictionary lists to my own table without create virtual foregion key in my static table.

Paweł Groński
  • 567
  • 1
  • 6
  • 17

1 Answers1

2

If a single user can have many cities, and the same city can be assigned to many users, this is a many-to-many relationship.

You can configure this in the ModelBuilder as follows:

ModelBuilder.Entity<UserWorker>()
   .HasMany(u => u.Cities)
   .WithMany()
   .Map(x => {
        x.MapLeftKey("UserWorkerId");
        x.MapRightKey("CityId");
        x.ToTable("UserWorkerToCityMap");
   });

EF will create an additional table UserWorkerToCityMap for this relationship that has two columns with foreign key references to UserWorkerId and CityId. No additional foreign key column will be added to DCity.

Configure Many-to-Many Relationship in Code-First

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
  • Thanks for the reply. I considered this option but if I have User table and another table that will have the same relation? User with --> Cities, Countries, Regions and EnotherTable --> with cities, Countries, Region? Than I need two table Many to Many? – Paweł Groński Mar 06 '18 at 13:04
  • No problem, just create the relationship twice with different names for the mapping tables , e.g. `UserWorkerToCityMap` and `OtherToCityMap` – Georg Patscheider Mar 06 '18 at 13:05
  • Than I will have 6 Map Tables: 3 map table for User (UserToCityMap, UserToRegionMap, UserToCountriesMap) and 3 map table foth enother own table. Correctly? – Paweł Groński Mar 06 '18 at 13:09
  • Yes exactly. But these tables will not take up much space because they only contain foreign keys. – Georg Patscheider Mar 06 '18 at 13:10
  • Ok. I understand. But is there a way to have one table for relation User with Dictionary static table (Contries, Region and Cities)? One table Map for three relation? – Paweł Groński Mar 06 '18 at 13:12
  • 1
    You could model the join table and use two one-to-many relationships instead of one many-to-many (many-to-many will always be managed by EF internally). See https://stackoverflow.com/a/7053393/1450855 – Georg Patscheider Mar 06 '18 at 13:23
  • The Map() method does not appear to exist anymore. – David Thielen Mar 09 '23 at 23:23