【问题标题】:EntityState.Modified without updating navigation propertyEntityState.Modified 不更新导航属性
【发布时间】:2017-01-26 19:41:13
【问题描述】:

我在使用 EntityState.Modified 更新我的模型之一时遇到了问题,因为它还尝试更新我的导航属性(我没有更改)。我的控制器操作是:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Rooms([Bind(Include = "ID,BedroomCount,BedroomsDescription,BathroomCount,BathroomsDescription")] Property property)
    {
        if (ModelState.IsValid)
        {
            //Modify property
            property.DateModified = DateTime.Now;
            property.Status = 1;
            db.Entry(property).State = EntityState.Modified;
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        return View(property);
    }

我的模型看起来像:

public class Property
{
    public int ID { get; set; }

    public Int16 BedroomCount { get; set; }

    public string BedroomsDescription { get; set; }

    public Int16 BathroomCount { get; set; }

    public string BathroomsDescription { get; set; }

    public int CommunityID { get; set; }

    public int PropertyTypeID { get; set; }

    public virtual Community Community { get; set; }
    public virtual PropertyType PropertyType { get; set; }
}

如您所见,我从我的 viewModel 中绑定了一堆东西(浴室和卧室数量等),但我根本没有修改导航属性“社区”,但每当我发布到这个操作方法时,我都会得到以下错误:

UPDATE 语句与 FOREIGN KEY 约束冲突 “FK_dbo.Property_dbo.Community_CommunityID”。冲突发生在 数据库“MyDatabase”,表“dbo.Community”,列“ID”。这 语句已终止。

如何在不尝试更新外键的情况下更新此模型?我在任何地方都找不到答案

【问题讨论】:

  • 我现在也面临这个问题,我使用 property.Community = null 解决了它。我知道这不是永久修复,但我也找不到答案
  • 是的,不幸的是我不希望我的社区为空。现在我也可以从我的视图模型中传递它,但这似乎真的没有必要,因为我不需要更改它
  • 是的,就我而言,我正在从我的实体特定的数据访问中更新我的实体,所以我可以知道更新 child 的调用无法到达那里。

标签: c# asp.net asp.net-mvc entity-framework


【解决方案1】:

您好,请您试一试,我希望它会奏效。

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Rooms([Bind(Include = "ID,BedroomCount,BedroomsDescription,BathroomCount,BathroomsDescription")] Property property)
{
    if (ModelState.IsValid)
    {
        // try this read this entoity from context and update properties you need to update
       var dbproperty = db.Property.FirstOrDefault(x=>x.Id= property.Id); // handle null 
        //Modify property
        dbproperty .DateModified = DateTime.Now;
        dbproperty .Status = 1;      

       // assuming in your context class Properties is the name of table.
         db.Entry(dbproperty ).State = EntityState.Modified;
        await db.SaveChangesAsync();
        return RedirectToAction("Index");
    }

    return View(property);
}

【讨论】:

  • 上面的代码看起来像是在尝试在数据库中创建一个新条目,但我想修改一个现有条目?
  • 如果您有任何问题,请尝试让我知道。我假设这一行已经存在,否则我们可以为 FirstOrDefault 处理 null 情况,正如我在回答中评论的那样
  • 这个“db.properties.Add(dbproperty)”明确表示它正在尝试将属性添加到上下文中,并且会通过一个异常“具有相同的实体已经存在”我相信
  • @AliBaig 感谢您指出我更新了我的答案并检查它现在正在更新当前记录。我还检查了子属性没有更新,因为没有变化。
  • @Gavin5511 抱歉,我错过了我现在检查并更新答案的那部分。
【解决方案2】:

将导航属性的状态也设置为EntityState.UnchangedEntityState.Modified

或者,在 db.Entry(property).State = EntityState.Modified; 之前将 nav 属性设置为 null。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-03
    • 2021-11-10
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    相关资源
    最近更新 更多