【问题标题】:Is there a better way to update my entities with the Entity framework?有没有更好的方法来使用实体框架更新我的实体?
【发布时间】:2011-01-05 00:17:06
【问题描述】:

我没有真正的“问题”,但我发现我开发这段代码的方式不是很好。

我有我的国家控制器(编辑方法)(WebUI 层):

    [HttpGet]
    public ActionResult Edit(int id)
    {
        var country = _groupsRepository.getCountryById(id);
        Mapper.CreateMap<Country, CountriesEditViewModel>(); 
        CountriesEditViewModel viewModel = Mapper.Map<Country, CountriesEditViewModel>(country);
        return View(viewModel);
    }
    //
    // POST: /CountriesAdmin/Edit/5
    [HttpPost]
    public ActionResult Edit(int id, CountriesEditViewModel viewModel)
    {
        try
        {
            if (ModelState.IsValid)
            {
                Mapper.CreateMap<CountriesEditViewModel, Country>();
                Country country = Mapper.Map<CountriesEditViewModel, Country>(viewModel);
                country.Name = IntranetTools.UppercaseFirst(country.Name.Trim());
                country.ISOCode = country.ISOCode.ToLower();
                _countryValidationService.UpdateCountry(country);
            }
        }
        catch (RulesException ex)
        {
            ex.CopyTo(ModelState);
        }

        if (ModelState.IsValid)
            return RedirectToAction("Index");
        else return View(viewModel);
    }

还有我的验证服务(域层):

    public void UpdateCountry(Country country)
    {
        EnsureValidForUpdate(country);
        // UPDATE
        var countryToUpdate = _groupsRepository.getCountryById(country.CountryId);
        countryToUpdate.CountryId = country.CountryId;
        countryToUpdate.Name = country.Name;
        countryToUpdate.ISOCode = country.ISOCode;
        _groupsRepository.SaveChanges();

    }

实际上,如您所见,我使用 Automapper 来映射我的 Country 实体(实体框架)和我的视图模型。 我使用验证服务进行验证并将我的对象(如果没有错误)更新到数据库。事实是我觉得我必须通过它的 ID 从数据库中获取我的对象来保存这个对象。我认为更新我的对象可能有更好的解决方案(因为我不想为我的对象映射所有字段并每次都从数据库中获取国家)

        var countryToUpdate = _groupsRepository.getCountryById(country.CountryId);
        countryToUpdate.CountryId = country.CountryId;
        countryToUpdate.Name = country.Name;
        countryToUpdate.ISOCode = country.ISOCode;
        _groupsRepository.SaveChanges();

是否有更好的解决方案来使用实体框架保存我的对象,或者我别无选择?

谢谢!

【问题讨论】:

    标签: .net entity-framework asp.net-mvc-2 entity


    【解决方案1】:

    通常您可以在不从数据库加载的情况下更新您的对象,但您必须知道它的 ID。

    您的更新函数可能如下所示:

    public void UpdateCountry(Country country)     
    {         
      EnsureValidForUpdate(country); 
      _objectContext.Attach(country);
    
      ObjectStateEntry entry = _objectContext.ObjectStateManager.GetObjectStateEntry(country);
      entry.SetModifiedProperty("Name");
      entry.SetModifiedProperty("ISOCode");
    
      _objectContext.SaveChanges();    
    } 
    

    我没有使用您的存储库,而是使用了 ObjectContext 实例。此代码要求您的 Country 实例已设置 Id、Name 和 ISOCode。更新将仅在 Name 和 ISOCode 字段上完成。

    但我不得不提一下,我没有使用这种方式。当您开始处理复杂的实体和关系时,在 EF 中首先加载实体是更好的方法。

    【讨论】:

    • 谢谢。我不知道这种方式。实际上,我不想先从数据库加载它,因为我认为它使用更多的资源来做到这一点..
    【解决方案2】:

    您可以循环调用此方法。然后循环调用 context.SaveChanges();

        public void UpdateCountry(Item updateItem)     
        {         
            context.YourDbObjects.Attach(updateItem);
    
            DbEntityEntry<Item> entry = context.Entry(updateItem);
    
            entry.State = EntityState.Modified;
        } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      相关资源
      最近更新 更多