确保实体框架始终从数据库读取?

发布于 2024-09-26 18:56:36 字数 332 浏览 10 评论 0原文

我有这个应用程序,它实际上是两个应用程序,一个网络应用程序和一个控制台应用程序。控制台应用程序用作 Windows 计算机上的计划任务,每天执行 3 次,以完成一些重复性工作。两个应用程序都使用放置在单独的项目(类库)中的相同模型和存储库。问题是,如果控制台应用程序需要对数据库进行一些更改,它会更新模型实体并将更改保存到数据库,但是当发生这种情况时,webb应用程序中的上下文不知道这一点,因此对象上下文不会使用新的/更新的数据和应用程序的用户无法看到更改。

我的问题是:有没有办法告诉 objectcontext 始终从数据库加载数据,无论是在洞 objectcontext 上还是针对特定查询?

/问候文布拉德

I have this applikation that is actually two applications, a webapplication and a console application. The console application is used as a scheduled task on the windows machine and is executed 3 times a day to to some recurring work. Both application uses the same Model and repository that is placed in a seperate projekt (class library). The problem is that if the console application need to make som changes to the database it updates the model entity and save the changes to database but when this happens the context in the webbapplication is unaware of this and therefore the object context is not refreshed with the new/updated data and the user of the application can not see the changes.

My question is: Is there a way to tell the objectcontext to always load data from the database, either on the hole objectcontext or for a specific query?

/Regards Vinblad

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

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

发布评论

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

评论(4

遮了一弯 2024-10-03 18:56:36

我认为你不应该在网络应用程序中遇到这个问题。 Web 应用程序中的 ObjectContext 应根据请求创建,因此只有更新期间的请求处理才会受到影响。

无论如何,很少有方法可以强制 ObjectContext 重新加载数据。查询和加载函数允许传递 MergeOption,它应该能够覆盖当前数据。但最有趣的应该是 Refresh 方法,尤其是 此应用程序

I don't think you should have this problem in web application. ObjectContext in web application should be created per request so only requests processing during update should be affected.

Anyway there are few methods wich can force ObjectContext to reload data. Queries and load functions allow passing MergeOption which should be able to overwrite current data. But the most interesting should be Refresh method especially with this application.

北凤男飞 2024-10-03 18:56:36

通过使用 DbSet,您还可以使用 .AsNoTracking() 方法。

By Using a DbSet you can you can also make use of the .AsNoTracking() method.

猫九 2024-10-03 18:56:36

每当您针对上下文运行类似

context.Entities.FirstOrDefault()

或任何查询时,数据实际上是从数据库中获取的,因此您应该不会遇到问题。

您在 Web 应用程序中的 ObjectContext 生命周期是多长? ObjectContext 是一个工作单元,因此应该仅创建它来获取/写入/更新数据,并在之后快速处理。
您可以在这里找到类似的问题:

刷新 ObjectContext 或重新创建它以反映对数据库所做的更改?

Whenever you run something like

context.Entities.FirstOrDefault()

or whatever query against the context, the data is actually fetched from the database, so you shouldn't be having a problem.

What is your ObjectContext lifetime in the webapp? The ObjectContext is a UnitOfWork, so it should be only created to fetch/write/update data and disposed quickly afterwards.
You can find a similar question here:

Refresh ObjectContext or recreate it to reflect changes made to the database?

少女情怀诗 2024-10-03 18:56:36

FWIW,在查询中创建一个新的(匿名)对象也会强制往返数据库:

' queries from memory    
context.Entities.FirstOrDefault()

' queries from db
context.Entities.Select(Function(x) New With {p.ID, p.Name}).FirstOrDefault()

请原谅 VB,这是我的母语语言:)

FWIW, creating a new (anonymous) object in the query also forces a round trip to the database:

' queries from memory    
context.Entities.FirstOrDefault()

' queries from db
context.Entities.Select(Function(x) New With {p.ID, p.Name}).FirstOrDefault()

Please forgive the VB, it's my native language :)

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