1

I am new to the C# Entity Framework (6). I created three classes - Country - Area - SubArea

There is a many-to-many relationship between Country and Area. There is another many-to-many relationship between Area and Subarea.

A Country can contain many Areas, but there also Areas that belong to more than one country). The same for Area and SubArea.

I created the respective classes and the database tables have been automatically created. The tables for CountryAreas and SubAreaAreas have been created too, so all looks good. Also the foreign keys look fine.

When I create a new Country Object with respective Areas and SubAreas adding to database works fine (see code below).

I now tried to add only a new Area (with new SubAreas) to an existing Country object, but I am clueless how this should work.

I fact I want to add the new Area object (or database entry) and let the Entity Framework update all the relations (and respective tables). The same also applies if I tried to add a new SubArea Object for an existing Area.

Any help is appreciated :-)

public class Country
{
   [Key]
   public string Name { get; set; }
   public List<Area> Areas { get; set; } // virtual enabled lazy loading
 }

public class Area
{
    [Key]
    public string Name { get; set; }
    public virtual List<SubArea> Subareas { get; set; }
    public virtual List<Country> Countries { get; set; }
}

public class SubArea
{
    [Key]
    public string Name { get; set; }
    public virtual List<Area> Areas { get; set; }
}

public class LocationScoutContext : DbContext
{
    public LocationScoutContext()
        : base("name=LocationScout")
    {
    }

    public DbSet<Country> Countries { get; set; }
    public DbSet<Area> Areas { get; set; }
    public DbSet<SubArea> SubAreas { get; set; }

}

// *** this works fine
internal static bool AddCountry(Country newCountry, out string errorMessage)
{
   bool success = true;
   errorMessage = string.Empty;

   try
   {
      using (var db = new LocationScoutContext())
      {
         db.Countries.Add(newCountry);
         db.SaveChanges();
      }
   }
   catch (Exception e)
   {
      errorMessage = BuildDBErrorMessages(e);
      success = false;
   }

   return success;
}

// *** this does not work
internal static bool UpdateCountry(Country upCountry, out string errorMessage)
{
   bool success = true;
   errorMessage = string.Empty;

   try
   {
      using (var db = new LocationScoutContext())
      {
         var country = db.Countries.SingleOrDefault(o => o.Name == upCountry.Name);
         country.Areas = upCountry.Areas;
         db.Entry(country).State = EntityState.Modified;

         var result = db.SaveChanges();
       }
    }
    catch (Exception e)
    {
       errorMessage = BuildDBErrorMessages(e);
       success = false;
    }

    return success;
}
  • Possible duplicate of [Add entity in many to many relationship in Entity Framework](https://stackoverflow.com/questions/47960981/add-entity-in-many-to-many-relationship-in-entity-framework) – Dean Kuga Jan 16 '19 at 19:28
  • @Dean Kuga: Maybe I get something wrong, but in the link question, there should be a completely new relationship inserted. I actually wanted to add just a new Area for an existing Country. – Bill Miller Jan 16 '19 at 20:17
  • I think this might be helpful https://stackoverflow.com/questions/27176014/how-to-add-update-child-entities-when-updating-a-parent-entity-in-ef – imk Jan 16 '19 at 20:24
  • @imk - Thanks for you answer - I checked the post but that sounds for me like a different topic – Bill Miller Jan 16 '19 at 21:22

0 Answers0