EF Core All CURD с ребенком работает нормально, но обновляется ребенок не работает - PullRequest
0 голосов
/ 10 марта 2019

У меня есть эти модели с контекстом Это работает все CURD с ребенком нормально, но когда обновление страны с городом только страна обновляется в SQL Server, но города не обновляются, почему? !!!

public class MyContext : DbContext 
    {
        public MyContext ()
        {

        }

        public MyContext (DbContextOptions<MyContext> options)
            : base(options)
        {
        }


        public DbSet<Country> Countries { get; set; }
        public DbSet<City> Cities { get; set; }
}


[Table("tblCountry")]
    public class Country
    {

        public Country()
        {
            this.Cities = new HashSet<City>();
        }

        [Key]
        [Column("countryID")]
        public int Id { get; set; }

        [Column("countryNo")]
        public string No { get; set; }

        [Column("countryADescr")]
        public string Name { get; set; }

        [Column("countryEDescr")]
        public string NameEn { get; set; }

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

        [Column("countryNote")]
        public string Note { get; set; }

}

[Table("tblCity")]
    public class City
    {
        [Column("ccId")]
        public int CountryId { get; set; } 

        [JsonIgnore]
        public virtual Country Country{ get; set; }

        [Column("cityId")]
        public string Id { get; set; } 

        [Column("cityAName")]
        public string Name { get; set; }
}

Добавить, удалить и выбрать отлично работать с ребенком

это api void update (put) Обновление только для страны, но дочерние города не обновлены

[HttpPut("{id}")]
        public async Task<IActionResult> PutCountrys(int id, Country country)
        {
            if (id != country.Id)
            {
                return BadRequest();
            }

            //_context.Countrys.Attach(country); // Must Not
            _context.Entry(country).State = EntityState.Modified;


            //City

    // Not work ??!!!
            //_context.Entry(country.Cities).State = EntityState.Modified;

            //foreach (var city in country.Cities)
            //{
            //    //_context.Entry(city).State = EntityState.Deleted;
            //    _context.Entry(city).State = EntityState.Modified;
            //}

    // Not work ??!!!
            ////var countryCity = await _context.Cities.SingleOrDefaultAsync(u => u.CountryId == country.Id);
            //var oldCities = await _context.Cities.Where(u => u.CountryId == country.Id).ToListAsync();
            //var oldCountry = await _context.Countrys.Include(i => i.Cities).SingleOrDefaultAsync(i => i.Id == id);
            //var oldCities = oldCountry.Cities;


    // Not work ??!!!
            ////_context.Entry(countryCity).State = EntityState.Modified;
            //if (oldCities.Any())
            //{
            //    foreach (var city in oldCities)
            //    {
            //        _context.Entry(city).State = EntityState.Deleted;
            //    }
            //}

    // Not work ??!!!
            //var newCities = country.Cities;
            //foreach (var city in newCities)
            //{
            //    if (city.CountryId > 0)
            //    {
            //        //_context.Cities.Attach(city);
            //        _context.Entry(city).State = EntityState.Modified;
            //    }
            //}

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CountrysExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

У меня есть эти модели с контекстом Это работает все CURD с ребенком нормально, но когда обновление страны с городом только страна обновляется в SQL Server, но города не обновляются, почему? !!!

...