简体   繁体   English

使用实体框架更新局部实体(带有剃刀视图)

[英]Updating partial entity using Entity Framework (with razor view)

I have an entity: 我有一个实体:

public class Organization
{
    public int OrganizationId { get; set; }
    [Required]
    [StringLength(160)]
    public string Name { get; set; }
    public DateTime? CreatedOn { get; set; }
    public DateTime? ModifiedOn { get; set; }
    public virtual ICollection<Team> Teams { get; set; }
    public virtual ICollection<ApplicationUser> Persons { get; set; }
}

I have created a OrganizationViewModel for representing data to view as well as creating new entities. 我已经创建了OrganizationViewModel来表示要查看的数据以及创建新实体。 I later map it using Automapper before saving changes to database using Entity Framework... 我稍后使用Automapper映射它,然后使用Entity Framework将更改保存到数据库中。

public class OrganizationViewModel
{
    public int OrganizationId { get; set; }
    [Required]
    [StringLength(160)]
    public string Name { get; set; }
    public List<ApplicationUser> Persons { get; set; }
}

I have created an action method Edit which should only allow user to edit property Name (for this example only, but in reality, it does update many other properties which I have slimmed down for this question).. 我创建了一个动作方法“编辑”,该方法仅应允许用户编辑属性Name(仅用于此示例,但实际上,它确实更新了我针对该问题而简化的许多其他属性)。

The Edit view only has two properties @Html.HiddenFor(model => model.OrganizationId) and @Html.EditorFor(model => model.Name) 编辑视图仅具有两个属性@Html.HiddenFor(model => model.OrganizationId) @Html.EditorFor(model => model.Name) @Html.HiddenFor(model => model.OrganizationId)@Html.EditorFor(model => model.Name)

Now in [HttpPost] 现在在[HttpPost]

I got OrganizationViewModel with updated Name property but property Persons remains null, if there are users already present in an organization EF throws an error as it is not expected by the context. 我获得了具有更新的Name属性的OrganizationViewModel ,但如果组织中已经存在用户,则Persons属性将保持为null EF抛出错误,因为上下文不希望这样做。

I could have updated the properties I want to update EXPLICITLY in Post method but I don't want to do everything manually. 我本来可以在Post方法中更新要明确更新的属性,但我不想手动执行所有操作。 Is there any other way where I can say that just update the entities I want. 还有什么其他方法可以说只更新我想要的实体。 I tried using Bind 's Include and Exclude but it has null for Persons as well. 我尝试使用BindIncludeExclude但对于Persons,它也为null

Help me :) 帮我 :)

If you create a view model that contains only the OrganizationId and Name properties that are edited by this view, you can achieve it by executing 如果创建的视图模型仅包含由该视图编辑的OrganizationId和Name属性,则可以通过执行以下操作来实现

db.Entry(db.Organizations
  .First(x => OrganizationId == viewModel.OrganizationId))
  .CurrentValues.SetValues(yourViewModel);

yourViewModel - can be any object with set of properties that you want to update, so you don't need to repeat your code but you have to have a specific view model for each operation. yourViewModel-可以是任何具有要更新的属性集的对象,因此您无需重复代码,但是每个操作都必须具有特定的视图模型。 You may have it even locally for the update method and automap to it but it must have only properties you want to update. 您甚至可能在本地将其用于更新方法并自动映射到它,但是它必须仅具有要更新的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM