EF 4.0模型缓存数据,不检测修改后的数据 [英] EF 4.0 model caching the data, and does not detect the modified data

查看:152
本文介绍了EF 4.0模型缓存数据,不检测修改后的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发ASP.NET应用程序,我有EF 4.0模型的问题。

I am developing ASP.NET application and I have problem with the EF 4.0 model.

EF模型检测新添加和删除的数据,但不会修改数据库中的数据。

The EF model detects the newly added and deleted data, but not the modified data from the database.

以下是我所遇到的问题的一个例子。

Here is an example of the problem what I have.

A-数据库:

生成Employees数据库表的脚本

Script to generate the "Employees" database table

CREATE TABLE [dbo].[Employees]
  (
   [id] [int] IDENTITY(1, 1)
         NOT NULL,
   [name] [nvarchar](50) NULL,
   CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED ( [id] ASC )
    WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
        IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
        ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
  )
ON [PRIMARY]

B-应用程序:

这是一个示例项目的链接点击这里

Here is a link for a sample project Click Here.

重现错误的步骤:

1-创建数据库并运行脚本创建表格。

1- Create the database and run the script to create the table.

2-在employees表中插入测试数据,并运行应用程序。数据将被加载到默认页面。

2- Insert test data in the employees table, and run the application. the data will be loaded in the default page.

3-更改连接字符串并运行应用程序。

3- Change the connection string and run the application.

3更新数据库中的一些值(直接形成sql)。并刷新页面

3- Update some values in the database (directly form the sql). and refresh the page

您会发现应用程序仍然显示旧数据,而如果从表中添加或删除项目,则它会分别从视图中添加或删除

You will find that the application still displaying the old data, while if you add or delete item from the table, it's added or removed from the view respectively.

提前感谢您的帮助。

推荐答案

基于ORM的基本概念的正确行为。它对于Linq到SQL也是一样的。这样做的原因是设计模式叫做IdentityMap,它确保了通过其键标识的每个实体只能在对象上下文中创建一次。所以你的第一个查询创建entites,但您的后续查询不会重新创建它们 - 它们已经存在。这个非常好的文章描述了这个问题的完整描述

This is correct behavior based on essential concepts of ORM. It also works same for Linq to SQL. The reason for this is design pattern called IdentityMap which ensures that each entity identified by its key is created only once for object context. So your first query creates entites but your subsequent queries don't recreate them - they already exists. The full description of this problem is described in this very nice article.

这篇关于EF 4.0模型缓存数据,不检测修改后的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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