是否有实体框架的更新对象持有? [英] Is there an Update Object holder on Entity Framework?

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

问题描述

我目前的插入/更新这样的字段(如果有一个更好的办法,请说出来 - 我们一直在学习)

I'm currently inserting/updating fields like this (if there's a better way, please say so - we're always learning)

public void UpdateChallengeAnswers(List<ChallengeAnswerInfo> model, Decimal field_id, Decimal loggedUserId)
{
    JK_ChallengeAnswers o;
    foreach (ChallengeAnswerInfo a in model)
    {
        o = this.FindChallengeAnswerById(a.ChallengeAnswerId);
        if (o == null) o = new JK_ChallengeAnswers();

        o.answer = FilterString(a.Answer);
        o.correct = a.Correct;
        o.link_text = "";
        o.link_url = "";
        o.position = FilterInt(a.Position);

        o.updated_user = loggedUserId;
        o.updated_date = DateTime.UtcNow;

        if (o.challenge_id == 0)
        {
            // New record
            o.challenge_id = field_id;  // FK
            o.created_user = loggedUserId;
            o.created_date = DateTime.UtcNow;

            db.JK_ChallengeAnswers.AddObject(o);
        }
        else
        {
            // Update record
            this.Save();
        }
    }

    this.Save(); // Commit changes
}

正如你所看到的还有2次 this.Save()(巫调用 db.SaveChanges() ;

添加:我们将新的对象变成占位符 ADDOBJECT 的方法,换句话说,新的对象不承诺马上,我们可以把我们想要尽可能多的对象。

when Adding we place the new object into a Place Holder with the AddObject method, in other words, the new object is not committed right away and we can place as many objects we want.

但是,当它是一个更新,我需要移动到下一个对象之前先保存,是没有办法,我可以为了使用,比方说一个方法:

But when it's an update, I need to Save first before moving on to the next object, is there a method that I can use in order to, let's say:

        if (o.challenge_id == 0)
        {
            // New record
            o.challenge_id = field_id;
            o.created_user = loggedUserId;
            o.created_date = DateTime.UtcNow;

            db.JK_ChallengeAnswers.AddObject(o);
        }
        else
        {
            // Update record
            db.JK_ChallengeAnswers.RetainObject(o);
        }
    }

    this.Save(); // Only save once when all objects are ready to commit
}

因此​​,如果有5次更新,我不知道的需要保存到数据库中5次,但只有一次在最后。

So if there are 5 updates, I don't need to save into the database 5 times, but only once at the end.

感谢你。

推荐答案

那么如果有一个被连接到图表对象时,如果您修改此目的的值,则该实体被标记为修改

Well if you have an object which is attached to the graph, if you modify values of this object, then the entity is marked as Modified.

如果你根本就 .AddObject ,则该实体将被标记为添加

If you simply do .AddObject, then the entity is marked as Added.

什么也没发生 - 只有分期图表

Nothing has happened yet - only staging of the graph.

然后,当你执行的SaveChanges(),EF将翻译的OSM相关存储查询的条目。

Then, when you execute SaveChanges(), EF will translate the entries in the OSM to relevant store queries.

您code看起来有点怪怪的。您已经通过调试(跑一个SQL跟踪),看看什么是真正得到执行?因为我看不出为什么你需要首先 .Save 因为内嵌我的以上几点,因为你修改的实体的方法的前几行,一个UPDATE声明最有可能的总是的得到执行,而不管的ID。

Your code looks a bit strange. Have you debugged through (and ran a SQL trace) to see what is actually getting executed? Because i can't see why you need that first .Save, because inline with my above points, since your modifying the entities in the first few lines of the method, an UPDATE statement will most likely always get executed, regardless of the ID.

我建议你重构你的code处理新/修改独立的方法。 (通过信息库的理想)

I suggest you refactor your code to handle new/modified in seperate method. (ideally via a Repository)

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

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