Solved

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

Posted on 2016-09-25
8
40 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 69

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 69

Expert Comment

by:Éric Moreau
ID: 41815948
what is db? Surely not an ObjectContext!
0
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 69

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 69

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now