protected void CreateDbContext() { DbContext = new AndaroDbContext(); //Do not enable proxies DbContext.Configuration.ProxyCreationEnabled = false; //Load navigation properties explicitly DbContext.Configuration.LazyLoadingEnabled = false; }
private static void CheckForEntititesWithoutStateInterface(AndaroDbContext context) { var entititiesWithoutState = from e in context.ChangeTracker.Entries() where !(e.Entity is IObjectWithState) select e; if (entititiesWithoutState.Any()) { throw new NotSupportedException("All entities must implement IObjectWithState"); } }
public static void ApplyChanges <TEntity>(TEntity root) where TEntity : class, IObjectWithState { using (var context = new AndaroDbContext()) { context.Set <TEntity>().Add(root); CheckForEntititesWithoutStateInterface(context); foreach (var entry in context.ChangeTracker.Entries <IObjectWithState>()) { IObjectWithState stateInfo = entry.Entity; entry.State = StateHelpers.ConvertState(stateInfo.State); if (stateInfo.State == State.Unchanged) { var databaseValues = entry.GetDatabaseValues(); entry.OriginalValues.SetValues(databaseValues); } } } }