Solved

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

Posted on 2016-09-25
8
44 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

14 Experts available now in Live!

Get 1:1 Help Now