处理非跟踪自跟踪实体的正确方法是什么?

发布于 2024-08-26 11:49:12 字数 1195 浏览 5 评论 0原文

自跟踪实体。惊人的。

除非您执行诸如

return Db.Users;

没有任何自跟踪实体正在跟踪之类的操作(直到它们可能被反序列化)。

美好的。因此,我们必须认识到,返回给我们的实体有可能没有启用跟踪。

现在怎么办?

我尝试过的事情

对于给定的方法体:

using (var db = new Database())
{
    if (update.ChangeTracker.ChangeTrackingEnabled)
        db.Configurations.ApplyChanges(update);
    else
        FigureItOut(update, db);

    db.SaveChanges();
    update.AcceptChanges();
}

FigureItOut的以下实现全部失败:

db.Configurations.Attach(update);
db.DetectChanges();

Nor

db.Configurations.Attach(update);
db.Configurations.ApplyCurrentValues(update);

Nor

db.Configurations.Attach(update);
db.Configurations.ApplyOriginalValues(update);

Nor

db.Configurations.Attach(update);
db.Configurations.ApplyChanges(update

Nor关于我能想到的任何东西,除了

  1. 获取 之外数据库中的原始实体
  2. 手动比较每个属性
  3. 根据需要更新属性

确切地说,我应该如何处理不跟踪自身的自我跟踪实体?


小更新:

盲目地将实体标记为修改作品,但这似乎有点臭。在这种情况下,这是我们能做的最好的事情吗?

Self tracking entities. Awesome.

Except when you do something like

return Db.Users;

none of the self-tracking entities are tracking (until, possibly, they are deserialized).

Fine. So we have to recognize that there is a possibility that an entity returning to us does not have tracking enabled.

Now what???

Things I have tried

For the given method body:

using (var db = new Database())
{
    if (update.ChangeTracker.ChangeTrackingEnabled)
        db.Configurations.ApplyChanges(update);
    else
        FigureItOut(update, db);

    db.SaveChanges();
    update.AcceptChanges();
}

The following implementations of FigureItOut all fail:

db.Configurations.Attach(update);
db.DetectChanges();

Nor

db.Configurations.Attach(update);
db.Configurations.ApplyCurrentValues(update);

Nor

db.Configurations.Attach(update);
db.Configurations.ApplyOriginalValues(update);

Nor

db.Configurations.Attach(update);
db.Configurations.ApplyChanges(update

Nor about anything else I can figure to throw at it, other than

  1. Getting the original entity from the database
  2. Comparing each property by hand
  3. Updating properties as needed

What, exactly, am I supposed to do with self-tracking entities that aren't tracking themselves??


Small update:

Blindly marking the entity as modified works, however this seems a bit smelly. Is it the best we can do in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九厘米的零° 2024-09-02 11:49:12

场景 1

以下是一些建议遵循的做法。
当您在 WCF 场景中使用 STE 时,您应该依赖 STE 实现的更改跟踪器,因此在服务器端执行以下操作。

db.Users.ApplyChanges(user);
db.SaveChanges();

场景2
但是,如果您在服务器上,建议的做法是在 objectcontext 的分部类上创建一个名为 EnableChangeTracking 的方法。该方法将查询处于未更改状态的实体,该实体实现了 IObjectWithChangeTracker 并打开更改跟踪,因此类似这样的操作

user = db.users.first(u => u.userid == 1);
db.EnableChangeTracking();

中检索的不同上下文中保存用户实体

db2.users.ApplyChanges(user);
db2.SaveChanges();

现在尝试从最初从场景 3
如果在服务器端您连接到从中检索用户实体的同一对象上下文,那么您可以使用 STE 作为简单的 poco 对象,如下面的

user = db.users.first(u => u.userid == 1);
user.LastName = "XYZ";
db.DetectChanges(); //no need for it cuz Savechanges implicitly calls this.
db.SaveChanges();

场景 4
如果从不同的上下文中检索用户实体,那么上下文您将使用它来保存,那么这里是另一个选项,您将实体标记为已修改,而不关心修改了什么。

user = db.users.first(u => u.userid == 1);
var db2 = new ObjectContext();
user.LastName = "XYZ";
db2.Users.Attach(user);
// i prefer this option..
db2.ObjectStateManager.ChangeObjectState(user,EntityState.Modified); 
db2.SaveChanges(); // updates all columns

场景5
如果从不同的上下文中检索用户实体,那么您将使用它来保存上下文,那么这里是您检索原始实体的另一个选项。

user = db.users.first(u => u.userid == 1);
user.lastName ="XYZ";
var db2 = new ObjectContext();
db2.Users.First(u => u.userid == user.userid);
db2.users.ApplyCurrentValues(user);
db2.SaveChanges();

这是一篇描述一些场景的博客文章。
http:// /weblogs.asp.net/zeeshanhirani/archive/2010/03/30/modifying-self-tracking-entity-on-the-server.aspx

我在我的实体框架 4.0 食谱书中广泛介绍了这些概念的场景..

scenario 1

Here are some recommended practices to follow.
When you are using STE in WCF scenario, you should rely on the change tracker that STE implements so on the server side you do the following.

db.Users.ApplyChanges(user);
db.SaveChanges();

scenario 2
However if you are on the server, the recommended practice is to create a method on the partial class for objectcontext called EnableChangeTracking. The method would query for entities that are in unchanged state that implements IObjectWithChangeTracker and turns on change tracking so something like this

user = db.users.first(u => u.userid == 1);
db.EnableChangeTracking();

now try to save the user entity from a different context from which it was originally retrieved from

db2.users.ApplyChanges(user);
db2.SaveChanges();

scenario 3
if on the server side you are connected to the same object context from which you retrieved the user entity from, then you use STE as simple poco object like below

user = db.users.first(u => u.userid == 1);
user.LastName = "XYZ";
db.DetectChanges(); //no need for it cuz Savechanges implicitly calls this.
db.SaveChanges();

scenario 4
if the user entity is retrieved from a different context then the context u will use it to save then here is another option where u mark the entity as modified and not care what got modified.

user = db.users.first(u => u.userid == 1);
var db2 = new ObjectContext();
user.LastName = "XYZ";
db2.Users.Attach(user);
// i prefer this option..
db2.ObjectStateManager.ChangeObjectState(user,EntityState.Modified); 
db2.SaveChanges(); // updates all columns

scenario 5
if the user entity is retrieved from a different context then the context u will use it to save then here is another option where u retrieve the original entity.

user = db.users.first(u => u.userid == 1);
user.lastName ="XYZ";
var db2 = new ObjectContext();
db2.Users.First(u => u.userid == user.userid);
db2.users.ApplyCurrentValues(user);
db2.SaveChanges();

Here is a blog post that describes few scenarios.
http://weblogs.asp.net/zeeshanhirani/archive/2010/03/30/modifying-self-tracking-entity-on-the-server.aspx

I extensively cover these concepts in my Entity Framework 4.0 recipes book with lots of scenarios..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文