sách gpt4 ai đã đi

c# - 使用 Web API, Entity Framework 如何知道对象发生了什么变化?

In lại 作者:行者123 更新时间:2023-11-30 20:55:48 28 4
mua khóa gpt4 Nike

我在 Web 应用程序中将 EF5 与 SQL Server 2012 结合使用。我有两个类(class):

public partial class Topic {
public Topic()
{
this.SubTopics = new List();
}
public int TopicId { get; set; }
public string Name { get; set; }
public int Number { get; set; }
public virtual ICollection SubTopics { get; set; }
}

public partial class SubTopic
{
public int SubTopicId { get; set; }
public int TopicId { get; set; }
public string Name { get; set; }
public string Notes { get; set; }
public virtual byte[] Version { get; set; }
public virtual Topic Topic { get; set; }
}

在我们的前端,我们对子主题注释进行了更改,然后当按下保存按钮时,主题对象及其子主题对象将作为 JSON 发送回 Web API Controller 。当我们检查发回的内容时,我们会看到 Notes 的新数据。当我们检查参数主题时,我们还会看到新的 Notes 数据。

public HttpResponseMessage PutTopic(int id, Topic topic)
{
_uow.Topics.Update(topic);
_uow.Commit();
}

但是通过 SQL Profiler 检查我们看不到任何改变子主题的事情。当数据被检索时,旧的子主题数据被返回并且对注释的编辑丢失。

对于像这样的 Web 更新的情况。 EF 如何确定已更改的内容?是否有某种方法可以让它检查/比较新对象的内容,以便它可以检测到更改并更新子主题?

配置:

    DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Configuration.LazyLoadingEnabled = false;
DbContext.Configuration.ValidateOnSaveEnabled = false;
DbContext.Configuration.ValidateOnSaveEnabled = true;
DbContext.Configuration.AutoDetectChangesEnabled = false;

更新方法:

    public virtual void Update(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State == EntityState.Detached)
{
DbSet.Attach(entity);
}
dbEntityEntry.State = EntityState.Modified;
}

1 Câu trả lời

自 ef4.x 以来? EF 具有检测更改功能。它可以在离线对象模式下为您工作。

我假设您的 UoW 处理没有按照您的意愿进行,因为对象在上下文中的放置方式以及状态的管理方式。

您的主要对象是附加一个对象,然后将其设置为状态已更改。但是所有的子对象呢?他们是否被加载到上下文中?自动检测更改是否需要检查?如果它不在上下文中,则 EF 看不到更改。

了解 EF 何时以及如何跟踪更改很重要 tracking changes in Poco Objects检查自动检测设置。

this.Configuration.AutoDetectChangesEnabled = false; ////<<<<<<<<< Default true

适用于 EF 的模式是:

var mypoco = Context.Set.Find(1); // find and LOAD your poco or sub object poco
// repeat for graph or use include etc...
// now in context and changes can be tracked. (see link)
// map json fields to poco entity....
myPoco.propertyXyz = json.ValuesSent; // some mapping approach to move your values, I use this package
//

// now tell EF things might have changed.
// normally not required by default, But incase your are not using tracking proxies , tell ef check for changes
// Context.Context.ChangeTracker.DetectChanges(); // uncomment when needed

Context.SaveChanged(); // will trigger detect changes in normal scenarios

EF 检查加载的实体是否有任何更改,它在首次附加时具有原始状态。所以它现在可以看到哪些属性发生了变化。它只会更新那些更改的实体,并且只会设置更改的属性。

关于c# - 使用 Web API, Entity Framework 如何知道对象发生了什么变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18076984/

28 4 0
Bài viết được đề xuất: Hành vi khác nhau của System.Timers.Timer và System.Threading.Timer
Bài viết được đề xuất: javascript - Jest 中的渲染方法
Bài viết được đề xuất: c - 该宏在什么情况下会导致错误?
Bài viết được đề xuất: c# - 我的数据表的过滤器
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com