在Entity Framework 6中更新子对象

法布里蒂奥·罗德里格斯(Fabricio Rodriguez)

(使用实体框架6.2)

我有以下两个模型/实体:

public class City
    {
        public int CityId { get; set; }
        public string Name { get; set; }
    }

public class Country
    {
        public Country()
        {
            Cities new HashSet<City>();
        }

        public int CountryId { get; set; }
        public string Name { get; set; }
        public virtual ICollection<City> Cities { get; set; }
    }   

和下面的DbContext

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

我的问题是:如果“国家”对象的子级(即城市)发生了变化,该如何更新?

我可以这样做吗?

List<City> cities = new List<City>();
// Add a couple of cities to the list...
Country country = dbContext.Countries.FirstOrDefault(c => c.CountryId == 123);
if (country != null)
{
    country.Cities.Clear();
    country.Cities = cities;
    dbContext.SaveChanges();
}

那行得通吗?还是我应该专门添加每个城市?即:

List<City> cities = new List<City>();
// Add a couple of cities to the list...
Country country = dbContext.Countries.FirstOrDefault(c => c.CountryId == 123);
if (country != null)
{
    country.Cities.Clear();
    foreach (City city in cities)
        country.Cities.Add(city);
    dbContext.SaveChanges();
}  
文基

您需要添加CitiesCountry要更新的特定对象。

public Country Update(Country country)
{
    using (var dbContext =new DbContext())
    {
        var countryToUpdate = dbContext.Countries.SingleOrDefault(c => c.Id == country.Id);
        countryToUpdate.Cities.Clear();
        foreach (var city in country.Cities)
        {
            var existingCity =
                dbContext.Cities.SingleOrDefault(
                    t => t.Id.Equals(city.cityId)) ??
                dbContext.Cities.Add(new City
                {
                    Id = city.Id,
                    Name=city.Name
                });

            countryToUpdate.Cities.Add(existingCity);
        }
        dbContext.SaveChanges();
        return countryToUpdate;
    }
}

更新:

  public class City
    {
        public int CityId { get; set; }
        public string Name { get; set; }

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

希望能帮助到你。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我可以使用Entity Framework版本6或7自动更新对象及其子对象吗?

如何使用 Entity Framework Core 中的另一个对象更新子列表

从Entity Framework 6中的集合中删除

EntityDataSource和Entity Framework 6

如何在Entity Framework 5中包含子对象的子对象

Entity Framework v6 中的 TPC

更新 Entity Framework Core 中的记录

从ID列表更新Entity Framework中的多行

在Entity Framework中嵌套嵌套

Entity Framework Core 中的 SqlGeometry

使用Entity Framework 6对多个对象进行SQL查询

使用Entity Framework 6提前加载孙对象

为什么要使用附件来更新Entity Framework 6?

通过 IDbCommandInterceptor 检测或传递 Entity Framework 6 和 Entity Framework Core 中的调用方法

如何仅在Entity Framework 6.1中加载子对象的某些字段?

在 Entity Framework Plus 中的 IncludeFilter 上使用 AND (&&) 运算符不会带回嵌套/子对象

Entity Framework 6产生的效率低下的查询

如何模拟Entity Framework 6异步方法?

使用Entity Framework更新行时发生异常

使用Entity FrameWork 7更新记录

在Entity Framework核心中更新多对多

Entity Framework Core 2的POCO关系更新

更新和编辑多对多表Entity Framework

在Entity Framework 6中使用存储库模式更新记录

实体未使用Entity Framework 6在数据库中更新

在Entity Framework Core 2.0中包括派生的子属性

Entity Framework Core中的动态查询执行

在Entity Framework Core中获取与条件的关系

Entity Framework Core 2.1 中的 ReverseEngineeringGenerator