簡體   English   中英

使用實體框架更新實體,同時忽略某些屬性

[英]Update an entity using entity framework while ignoring some properties

我正在使用 asp.net mvc 4。我必須使用編輯方法更新我的持久性存儲,但我想忽略一些列。

我在這里找到了一些答案,但它們對我不起作用(我想)。

這是我的方法:

[HttpPost]
public ActionResult Edit(Candidat candidat)
{
    ProcRecDB _db = new ProcRecDB();  //  from DbContext

    if (ModelState.IsValid)
    {

        _db.Entry(candidat).State = EntityState.Modified;
        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(candidat);
}

候選模型有 10 個屬性; 我將如何忽略其中一些?

如果您使用 EF 5,則可以在將屬性標記為已修改后將其標記為未修改

_db.Entry(candidat).State = EntityState.Modified;
// Ignore changes to the value of SomeProperty
_db.Entry(candidat).Property("SomeProperty").IsModified = false;
_db.SaveChanges();

您可以創建一個新對象,將其附加到 db 上下文,然后只更新您想要保留的屬性。

[HttpPost]
public ActionResult Edit(Candidat candidat)
{
    ProcRecDB _db = new ProcRecDB();  //  from DbContext

    if (ModelState.IsValid)
    {
        var updatedCandidat = new Candidat { Id = candidat.Id };

        _db.Attach(updatedCandidat);

        // Set the properties that you would like to update. This must be
        // done after the object has been attached to the db context.
        updatedCandidat.FirstName = candidat.FirstName;
        updatedCandidat.LastName = candidat.LastName;
        ...

        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(candidat);
}

你在同一個位置有我。 我有類似的事情要做。

你必須選擇:

您可以使用 NotMapped (以防您不想存儲任何值)。

但是我認為你想要這個:

如果它是只讀的,並且您不想修改,那么您可以執行類似的操作:

var attachedEntity = this.context.Entry(orignalEntity);
                    attachedEntity.CurrentValues.SetValues(updatedEntity);

    List<string> excludeProperties = new List<string>();

                        // Some of these fields you cannot just modify at all.
                        excludeProperties.Add("UniqueIdentifier");
                        excludeProperties.Add("AuthorID");
                        excludeProperties.Add("DateCreated");
                        // You could even ask your dervived calls to tell which one to exclude 
// excludeProperties.AddRange(this.ExcludeUpdateProperties());

                        foreach (var name in excludeProperties)
                        {
                            var property = attachedEntity.Property(name);
                            if (property != null)
                            {
                                attachedEntity.Property(name).IsModified = false;
                            }
                        }

使用這種方法,您可以使用 attachEntity.CurrentValues.SetValues(updatedEntity) 而不是更新那些需要更新的字段,它將所有值設置為新值,然后您可以排除您想要的。 這種方法比一個一個地更新每個字段要好。

假設您有一個具有 10 個屬性的對象,而您只想更新一個。 首先,正如預期的那樣,客戶端的 .cshtml 表單應該只包含您正在更新的字段以及 ID。 其次,在 post 方法中執行以下操作:

// Form Data Model
public Candidate FormCandidate {get; set;}

public IActionResult OnPostCanidata() {
    var _update = _context.Candidate.Find(Id);
    _update.SomeProperty = FormCandidate.SomeProperty;
    _context.SaveChanges();
}

我更喜歡這種方法而不是“Property/isModified”方法,因為如果 10 個字段中有 9 個沒有得到更新,那么需要編寫大量額外的服務器端代碼。 此外,如果數據模型發生更改並添加了其他只讀屬性,則無需重新訪問代碼並指定其 Property/isModified=fasle。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM