Linq2SQL: refresh an entity instance, when changed on the database

One of the scenario that become when we work with ORMs, is the data reliability. There are any cases in which the data we are consuming, are prior changed from another user or application, and we need to sync this data before to use it.

Supposing we are using Linq2SQL, we could resolve this issue, simply executing a refresh on the instance (or the instances) that we are working on, to reload any or all the values from the database onto our instance.

The following is an example:

// myDataContext is a Linq2SQL Data Context

myDataContext ctx = new
myDataContext();

 

myEntityType instance = ctx.myEntityTypeSet.Where(set => set.ID == 1).Single();

 

// this is the refresh statement

ctx.Refresh(RefreshMode.KeepChanges, instance);

 

Values that can be assigned to RefreshMode enumeration are the following:

We can define the this parameter, in order to preserve the local changed values, or reload all values from database, overwriting local changes.

Linq2SQL: refresh an entity instance, when changed on the database

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...