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

查看:22
本文介绍了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- 应用程序:

这是示例项目的链接单击此处.

重现错误的步骤:

1- 创建数据库并运行脚本以创建表.

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

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

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 to SQL.这样做的原因是称为 IdentityMap 的设计模式,它确保由其键标识的每个实体仅为对象上下文创建一次.因此,您的第一个查询会创建实体,但您的后续查询不会重新创建它们 - 它们已经存在.这个问题的完整描述在这篇非常好的文章中进行了描述.

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天全站免登陆