private static string CreateHistoryOnUpdateSnippet(EntityHistoryInfo info) { return(string.Format( @" if (insertedNew.Count() > 0 || updatedNew.Count() > 0) {{ var now = SqlUtility.GetDatabaseTime(_executionContext.SqlExecuter); const double errorMarginSeconds = 0.01; // Including database DataTime type imprecision. foreach (var newItem in insertedNew.Concat(updatedNew)) if (newItem.ActiveSince == null) newItem.ActiveSince = now; if (updatedNew.Count() > 0 && !(updatedNew is Common.DontTrackHistory<{0}.{1}>)) {{ var createHistory = updatedNew.Zip(updated, (newItem, oldItem) => new {{ newItem, oldItem }}) .Where(change => (change.oldItem.ActiveSince == null || change.newItem.ActiveSince > change.oldItem.ActiveSince.Value.AddSeconds(errorMarginSeconds))) .Select(change => change.oldItem) .ToArray(); _domRepository.{0}.{1}_Changes.Insert( createHistory.Select(olditem => new {0}.{1}_Changes {{ ID = Guid.NewGuid(), EntityID = olditem.ID{2} }}).ToArray()); }} }} ", info.Entity.Module.Name, info.Entity.Name, ClonePropertiesTag.Evaluate(info))); }
/// <summary> /// Creates a DateTime filter that returns the "Entity"_Changes records that were active at the time (including the current records in the base entity). /// AllProperties concept (EntityHistoryAllPropertiesInfo) creates a similar filter on the base Entity class. /// </summary> private static string FilterImplementationSnippet(EntityHistoryInfo info) { return(string.Format( @"public global::{0}.{1}[] Filter(System.DateTime parameter) {{ var sql = ""SELECT * FROM {2}.{3}(@p0)""; var query = _executionContext.EntityFrameworkContext.Database.SqlQuery<{0}.{1}>(sql, parameter); return query.ToArray(); }} ", info.Dependency_ChangesEntity.Module.Name, info.Dependency_ChangesEntity.Name, SqlUtility.Identifier(info.Entity.Module.Name), SqlUtility.Identifier(info.Entity.Name + "_AtTime"))); }