Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

static - EF 4.0 model caching the data, and does not detect the modified data

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

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- Database:

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- Application:

Here is a link for a sample project Click Here.

Steps to reproduce the error:

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

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

3- Change the connection string and run the application.

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.

Thanks in advance for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...