简体   繁体   中英

How do you connect a 1 to many table

I have a ASP.NET Core project that has these two tables: City and Country.

public class City 
{
    [Key]
    public int Id { get; set; }
    public string CityName{ get; set; }
    public string Region{ get; set; }
    public int CountryID { get; set; }
    public Country Country { get; set; }
}

public class Country
{   
    [Key]
    public int Id { get; set; }
    [Required]
    public string CountryName{ get; set; }

    [Required]
    public string Continent{ get; set; }

    [Required]
    public string Capital{ get; set; }
    [Required]
    public int NumberOfPeople{ get; set; }
}

A city class inherits a country foreign key and 1 country can have more than one city ie 1-(0..M). They both come from the same DbContext .
I want to be able to click on the details page on the index page of "Countries"(display all the countries, their attributes and options) so that it show the country info and all the cities of that country but I can't figure out how to connect the two tables via the DbContext .

How do I pass a model that loads the Country info with the specific id and all the cities inside to the Details View

If you want to include all City s in a Country , you will need to add a navigational property from Country to City (the other part of the already configured relationship):

public class Country
{
    ...
    public ICollection<City> Cities { get; set; }
}

This allows you to perform a single database query to bring all the results without having to do manual joins.
You can then use this as:

var country = await context.Countries
    .Include(x => x.Cities)
    .SingleAsync(x => x.ID == someId);

You can use the non- Async versions of the query executors, but given that you are on an ASP.NET Core app, I'd recommend against that.

One way is to use attributes for the fields:

public class City
{
    [Key]
    public int Id { get; set; }
    public string CityName{ get; set; }
    public string Region{ get; set; }
    public int CountryID { get; set; }

    [ForeignKey("CountryID")]
    public virtual Country Country { get; set; }
}

public class Country
{   
    [Key]
    public int Id { get; set; }

    [Required]
    public string CountryName{ get; set; }

    [Required]
    public string Continent{ get; set; }

    [Required]
    public string Capital{ get; set; }

    [Required]
    public int NumberOfPeople{ get; set; }

    [InverseProperty("Country")]
    public virtual ICollection<City> Cities { get; set; }
}

There are multiple ways to do it.

First, you should declare your classes, such as:

public class City 
{
    [Key]
    public int Id { get; set; }

    public string CityName{ get; set; }

    public string Region{ get; set; }

    public int CountryId { get; set; }

    public Country Country { get; set; }
}

public class Country
{   
    [Key]
    public int Id { get; set; }

    [Required]
    public string CountryName{ get; set; }

    [Required]
    public string Continent{ get; set; }

    [Required]
    public string Capital{ get; set; }

    [Required]
    public int NumberOfPeople{ get; set; }

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

Second, you should configure the relationship:

protected override void OnModelCreating(Modelbuilder modelBuilder)
{
    modelBuilder.Entity<Country>()
        .HasMany(c => c.Cities)
        .WithRequired(e => e.Country)
        .HasForeignKey(c => c.CountryId);
}

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