简体   繁体   English

如何在EF Core中相关集合中的属性上将IsModified设置为false?

[英]How to set IsModified to false on a property in a related Collection in EF Core?

I am using Asp.Net Core 1.1 and I have two classes: 我正在使用Asp.Net Core 1.1,并且有两个类:

public class Scale
{
    [Key]
    public int ScaleId { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public decimal DefaultValue { get; set; }

    public List<ScaleLabel> Labels { get; set; }
}

public class ScaleLabel
{
    [Key]
    public int ScaleLabelId { get; set; }

    public int ScaleId { get; set; }
    public virtual Scale Scale { get; set; }

    public decimal Value { get; set; }

    public string Label { get; set; }
}

When a scale is used, all its ScaleLabels should be prohibited to update except for their Label property. 使用比例尺时,除其Label属性外,应禁止其所有ScaleLabel更新。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, [Bind("ScaleId,Name,Description,DefaultValue,Labels")] Scale scale)
    {
        if (id != scale.ScaleId)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                if (IsScaleUsed(id))
                {
                    _context.Scales.Attach(scale);
                    _context.Entry(scale).Collection(c => c.Labels).IsModified = false;
                }
                else
                {
                    _context.Update(scale);
                }
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ScaleExists(scale.ScaleId))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction("Index");
        }
        return View(scale);
    }

If I use _context.Entry(scale).Collection(c => c.Labels).IsModified = false; 如果我使用_context.Entry(scale).Collection(c => c.Labels).IsModified = false; then nothing is updated, and if I don't use it, then all ScaleLabels are updated. 然后什么都没有更新,如果我不使用它,那么所有ScaleLabel都会更新。 I want to specify which properties of the Scale's Labels navigation property are modified and which are not. 我想指定“缩放”的“标签”导航属性的哪些属性被修改,哪些未被修改。

Rather than playing with IsModified property of the related CollectionEntry , you need to use the IsModified property of the PropertyEntry returned by Property method (or Properties property) of EntityEntry for each element of the related collection (basically the same way as you would do for specific property of any entity). 而不是打IsModified相关的财产CollectionEntry ,您需要使用IsModified的财产PropertyEntry通过返回Property方法(或Properties的属性) EntityEntry对相关集合的每一个元素(基本相同的方式,你会具体做任何实体的财产)。

In other words, instead of 换句话说,代替

_context.Entry(scale).Collection(c => c.Labels).IsModified = false;

you would use something like this: 您将使用以下内容:

foreach (var label in scale.Labels)
    foreach (var p in _context.Entry(label).Properties.Where(p => p.Metadata.Name != "Label"))
        p.IsModified = false;

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

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