0

I'm new to entity framework, and I wasn't able to find an answer to my question online, so that is why I decided to post my question here.

I added 3 tables to my model, and I thought what it does is only Mapp the tables with the corresponding entity and everytime I use linq to sql it would just go to the database and retrieve the data I need, but I think I'm wrong because I Updated one table(using SQL Server) and then I ran the application and I realized it still had the old data.

So my guess is that edmx tables are stored in memory and they do not update unless I told them to do so?, am I right or am I doing something wrong?

Some code:

 public List<Setting> SetGeneralSettings()
    {
      List<Setting> GeneralSettings = null;
        using (var Entity = new Entities())
        {
            //Entity.GeneralSettings_Skills.
            GeneralSettings = (from settings in Entity.Table1
                                             select new Setting
                                            {
                                                Property1 = settings.Property1,
                                                Property11 = settings.Property11,
                                                Property12 = settings.Property12,
                                                Property14 = settings.Property14,
                                                Property15 = settings.Property15,
                                                Property16 = settings.Property16,
                                                Property17 = settings.Property17,
                                                Property18 = settings.Property18,
                                                Property19 = (from o in Entity.Table2
                                                                 where o.IdTable2 == settings.IdTable1
                                                                 select o.valTable2).ToList(),
                                                SkillsList2 = (from s in Entity.Table3
                                                              where s.IdTable3 == settings.IdTable1
                                                              select s.valTable3).ToList()

                                            }).Where(p => p.Project == "Test project").ToList();
        }
            return GeneralSettings;
    }
user3044096
  • 171
  • 1
  • 13

4 Answers4

1

You can easily use ur connecition class that derived from ObjectContext instead of using sql server commands. I wrote that derived class as ObjectContext . After entering data and then call for example

                  context.SaveChanges();

and if u want to refresh data u can use

              using(ObjectContext context1 = new ObjectContext())
              {
               exampleBindingSource.DataSource  = context1.Example;
              }

You can make refresh like this. If i am correct to understand u :)

husonos
  • 231
  • 2
  • 14
0

From @AkashaKava

EF will not load changes unless you re query the context. EF queries db and loads maps them into objects, it watches changes you perform on objects and not on the database. EF does not track changes made directly to database and it will never track.

You have loaded a List, that List is your cache in memory. Even calling Save Changes will not refresh. You will have to query the context once again, that is create new list.

To see changes You will have to execute following line once more,

datamodel.Compliances.Where(c => c.School.DistrictId == districtId).ToList()

Entity Framework Caching Issue

. . .

From @Morteza

When you use EF it by default loads each entity only once per context. The first query creates entity instace and stores it internally. Any subsequent query which requires entity with the same key returns this stored instance. If values in the data store changed you still receive the entity with values from the initial query

A careful answer: https://stackoverflow.com/a/3653392/1863179

Community
  • 1
  • 1
HouseCat
  • 1,155
  • 13
  • 18
  • I'm Using this code: using (var entity = new Entities()) {....}, isn't that a new instance of the DBContext since Entities: DBContext? – user3044096 Jan 29 '15 at 14:25
  • If changes aren't being committed, new var entity gets the context at initialization because changes are applicable to the instance of that object and only that object. You would need to refresh the context/append changes. – HouseCat Jan 29 '15 at 17:07
0

Entity framework holds previous results cached per instance of a DBContext (may also be the same for ObjectContext), which for example means if you are running the same linq to sql query within the same DBContext scope you may get cached results; although SQL is sent to the db server per linq to sql query the DBContext result may not be freshly updated.

Make sure you re instantiate DBContext before re-querying the database.

Also mentioned in more detail on EF4 and Caching...

Community
  • 1
  • 1
Chris du Preez
  • 529
  • 3
  • 8
0

Entity Framework's (EF from here on in) DbContext are not shared instances. Each one you create holds its' own changes in memory and won't write to the database until you call SaveChanges.

Since you are creating a new context each time, there shouldn't be anything that is stored in the cache of the context. EF does store retrieved records in a cache for quicker access

You don't offer a lot of context around the usage, but you can setup debugging points to see if the data is indeed stale or, if using a web application, it could be caching the resulting view and re-displaying it vs the data actually being stale. Maybe you aren't re-binding the data properly.

You should also keep in mind that contexts should have a relativity short lifetime. If you are running into caching issues, you probably are letting the context live too long.

Justin
  • 3,289
  • 3
  • 14
  • 26