如何使用实体框架7更新记录? [英] How to update record using Entity Framework 7?

查看:201
本文介绍了如何使用实体框架7更新记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新使用EntityState.Modified但调用SaveChanges()各类相关表不应用更改的字段。我使用的邮递员张贴到本地主机/ 8000 / API / CO / 1和状态说202所接受。我的存储库返回的记录,但它不是在所有的修改。

什么是我在我的仓库丢失的变化采取影响?
我是否需要单独设置每个属性? (我有几百个)

大多数我能找到相关的资料库模式和实体框架7,仅利用EntityState.Modified(例子),然后保存更改。有人可以帮我指出我缺少什么?我有我的其他存储库工作正常的创建,删除和获得工作就好了。

我的库

 公共COMP更新(INT ID)
{
    VAR原= _context.Complaints.Where(C => c.COMP_ID == ID).FirstOrDefault<投诉>();
    _context.Entry(原件).STATE = EntityState.Modified;
    保存全部();
    返回原来的;
}公共BOOL白水回收()
{
    返回_context.SaveChanges()> 0;
}


解决方案

我不明白,你实际上是做出实体的任何变化都没有。你应该拉原始的实体,进行更改,然后调用的SaveChanges()提交它们:

 公共COMP更新(INT ID,字符串名称)
{
    //取得你的实体
    VAR原= _context.Complaints.FirstOrDefault(C => c.COMP_ID == ID);
    //此处进行更改(使用参数)
    original.Name =名称;
    //保存更改
    保存全部();
    //返回原来的实体所做的更改
    返回原来的;
}

在上述情况下,假设你使用邮差来更新这个特殊的实体,并在 ID通过名称作为参数(例如目的,我们将假定此方法只是更新的名称)。

如果你这样做,那么你可以使用名称参数来更新现有的实体,例如:

  //进行更改这里(使用参数)
original.Name =名称;

然后调用的SaveChanges()方法实际上将在数据库中执行更新。

如果您已经在环境启用更改跟踪,你不需要担心手工设置或者修改的状态。

I am trying to update a field with various related tables using the EntityState.Modified but SaveChanges() is not applying the changes. I am using postman to post to the localhost/8000/api/co/1 and the status says 202 accepted. My repository is returning the record but it is not modified at all.

Question What am I missing in my repository for the changes to take in affect? Do I need to set each property individually? (I have hundreds)

Most the examples I can find related to the repository pattern and the entity framework 7, only utilize the EntityState.Modified(), then save changes. Can someone please help me point out what I am missing? I have my other repositories working fine that create, delete and get working just fine

My Repository

public COMP Update(int id)
{
    var original = _context.Complaints.Where(c => c.COMP_ID == id).FirstOrDefault<COMPLAINT>();
    _context.Entry(original).State = EntityState.Modified;
    SaveAll();
    return original;
}

public bool SaveAll()
{
    return _context.SaveChanges() > 0;
}

解决方案

I don't see where you are actually make any changes to entity at all. You should be pulling the original entity, make your changes and then call SaveChanges() to commit them :

public COMP Update(int id, string name)
{
    // Grab your entity
    var original = _context.Complaints.FirstOrDefault(c => c.COMP_ID == id);
    // Make your changes here (using a parameter)
    original.Name = name;
    // Save your changes
    SaveAll();
    // Return your original entity with the changes made
    return original;
}

In the above scenario, let's say you used Postman to update this particular entity and passed in the id and name as parameters (for example purpose, we will assume that this method just updates the name).

If you do that, then you could use the name parameter to update your existing entity as such :

// Make your changes here (using a parameter)
original.Name = name;

Then calling the SaveChanges() method will actually perform the update in your database.

If you have Change Tracking enabled on your context, you shouldn't need to worry about manually setting the modified states either.

这篇关于如何使用实体框架7更新记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆