Solved

C#: How to refresh Entity Framwork 6 entity list?

Posted on 2016-09-25
8
48 Views
Last Modified: 2016-09-29
I've got a class library that includes a static Account entity (it's called by an external program so almost everything has to be static). Account has a list of Alert entities among other things. I've now added a website where I can add, update and delete Alerts for each account to the database, but I'm struggling with figuring out how the class library should reload the Alerts on a timer.

Basically what I want to do is something like myaccount.Alerts.Reload(), but there is no Reload (or Refresh) function in EF 6.

Any suggestions?
TIA!
Dennis
0
Comment
Question by:Dennis_Gundersen
  • 4
  • 4
8 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41815937
You can call the Refresh method of your object context as shown in https://msdn.microsoft.com/en-us/library/bb896255(v=vs.110).aspx:

YourObjectContext.Refresh(RefreshMode.StoreWins, YourEntity);

Open in new window

0
 

Author Comment

by:Dennis_Gundersen
ID: 41815942
Unfortunately, that function is gone in EF 6. db.Refresh(...), simply doesn't show up in my project. It must have been supplanted by something else, but I can't find it anywhere.

Re
Dennis
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41815948
what is db? Surely not an ObjectContext!
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:Dennis_Gundersen
ID: 41815952
Yes, "db" is my static variable for the DbContext. I believe the ObjectContext has been replaced some time ago.

Re
D
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41815956
A DbContext is not an ObjectContext! Convert it like this:
var context = new YourDbContext();
var adapter = (IObjectContextAdapter)context;
var objectContext = adapter.ObjectContext;

Open in new window

0
 

Author Comment

by:Dennis_Gundersen
ID: 41815976
OK, that's smart, but I still see one problem. I don't want to update the complete Account entity, since that would entail loading potentially millions on rows of Result and Order entities for that Account as well. I just want to reload myaccount.Alerts (which would probably be 5-6 rows only).
I'm thinking it may be possible with something like this:

            using (var context = new HGContext())
            {
                var adapter = (IObjectContextAdapter)context;
                var objectContext = adapter.ObjectContext;
                objectContext.Refresh(System.Data.Entity.Core.Objects.RefreshMode.StoreWins, acc.Alerts);
            }

Open in new window

I'm then assuming that this would take my static Account entity and only refresh it's Alerts collection. Am I understanding this correctly?

Re
D
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41815990
I have situations where I refresh only one entity (table). What you wrote (acc.Alerts) should work. If you want to be sure of what is getting refreshed, open the SQL Profiler to see the query.
0
 

Author Comment

by:Dennis_Gundersen
ID: 41815995
Alright then, this sounds very promising. It's going to take me a while to actually test this out completely, but I'm hopeful and will just add another question of something should fail with the finer details.
Thank you very, very much!
Re
Dennis
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question