Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4.1 - Refresh is not a member of Context

I'm trying to revert Context changes using the Context.Refresh method but It seems like Refresh is not a member of Context.

I'm using the Microsoft ADO.NET Entity Framework 4.1 RC version.

Any idea?

like image 300
tebi Avatar asked Oct 22 '11 23:10

tebi


People also ask

How do you refresh entities?

To refresh an entity definition: Right-click the Entities folder and select Refresh Entity.

What is DbContext and DbSet?

DbContext generally represents a database connection and a set of tables. DbSet is used to represent a table. Your code sample doesn't fit the expected pattern.

What is the DbContext in EF?

A DbContext instance represents a session with the database and can be used to query and save instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns. Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance.

How do I change the state of entity using DB context?

This can be achieved in several ways: setting the EntityState for the entity explicitly; using the DbContext. Update method (which is new in EF Core); using the DbContext. Attach method and then "walking the object graph" to set the state of individual properties within the graph explicitly.


2 Answers

You are likely looking at a DbContext, which does not have a Refresh method. You can use the IObjectContextAdapter interface to get the underlying ObjectContext, and call Refresh on that.

var objectContext = ((IObjectContextAdapter)context).ObjectContext;
like image 127
Jeff Ogata Avatar answered Nov 03 '22 17:11

Jeff Ogata


You can also use the Reload function on the Proxy Objects... Here is a sample to reload all modified objects:

            var modifiedEntries = context.ChangeTracker.Entries()
                .Where(e => e.State == EntityState.Modified);
            foreach (var modifiedEntry in modifiedEntries) {
                modifiedEntry.Reload();
            }
like image 2
Markus Avatar answered Nov 03 '22 15:11

Markus