?
Solved

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

Posted on 2016-09-25
8
Medium Priority
?
64 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

765 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