public static object[] AddEntity(DbAccessLayer mgr, int number, Type poco, Action <object> defaulting = null) { mgr.RaiseEvents = false; var typeCache = mgr.GetClassInfo(poco); if (typeCache.PrimaryKeyProperty == null) { throw new NotSupportedException("Please provide a PK for that Entity"); } var users = new List <object>(); mgr.Database.RunInTransaction((dd) => { for (var i = 0; i < number; i++) { var user = typeCache.New(); if (defaulting != null) { defaulting(user); } users.Add(mgr.InsertWithSelect(poco, user)); } }); mgr.RaiseEvents = true; return(users.Select(f => typeCache.PrimaryKeyProperty.Getter.Invoke(f)).ToArray()); }
/// <summary> /// Sync the Changes to this Collection to the Database /// </summary> public void SaveChanges(DbAccessLayer layer) { var bulk = layer.Database.CreateCommand(""); var removed = new List <T>(); foreach (var pair in _internalCollection) { IDbCommand tempCommand; switch (pair.Value) { case CollectionStates.Added: tempCommand = layer.CreateInsertWithSelectCommand(typeof(T), pair.Key); break; case CollectionStates.Removed: tempCommand = DbAccessLayer.CreateDelete(layer.Database, layer.GetClassInfo(typeof(T)), pair.Key); removed.Add(pair.Key); break; case CollectionStates.Unchanged: tempCommand = null; break; case CollectionStates.Changed: tempCommand = layer.CreateUpdate(pair.Key, layer.Database); break; default: throw new ArgumentOutOfRangeException(); } if (tempCommand != null) { bulk = layer.Database.MergeCommands(bulk, tempCommand, true); } } var results = layer.ExecuteMARS(bulk, typeof(T)).SelectMany(s => s).Cast <T>().ToArray(); //Added var added = _internalCollection.Where(s => s.Value == CollectionStates.Added).ToArray(); for (var i = 0; i < added.Length; i++) { var addedOne = added[i]; var newId = results[i]; DataConverterExtensions.CopyPropertys(addedOne.Value, newId, layer.Config); } //Removed foreach (var item in removed) { _internalCollection.Remove(item); } foreach (var collectionStatese in _internalCollection.Keys.ToArray()) { ChangeState(collectionStatese, CollectionStates.Unchanged); } }
public static TPk[] AddEntity <T, TPk>(DbAccessLayer mgr, int number, Action <T> defaulting = null) where T : class { var typeCache = mgr.GetClassInfo(typeof(T)); if (typeCache.PrimaryKeyProperty.PropertyType != typeof(TPk)) { throw new NotSupportedException( $"Wrong Generic for that Entity. Expected was {typeof(TPk)} but was {typeCache.PrimaryKeyProperty.PropertyType}"); } return(AddEntity(mgr, number, typeof(T), o => { if (defaulting != null) { defaulting(o as T); } }).Select(f => (TPk)f).ToArray()); }
/// <summary> /// Wraps a /// <paramref name="query" /> /// on a given typeof(T) by including /// <paramref name="entry" /> /// 's /// propertys that are defined in /// <paramref name="propertyInfos" /> /// </summary> /// <returns></returns> public static IDbCommand CreateCommandWithParameterValues <T>(this DbAccessLayer db, string query, string[] propertyInfos, T entry) { return(db.Database.CreateCommandWithParameterValues(db.GetClassInfo(typeof(T)), query, propertyInfos, entry)); }