public static List <TObject> GetModels <TObject>() where TObject : class, IEntityObject <TObject>, new() { using (var context = new ModelsDbContext()) { try { var obj = new TObject(); return((obj.HasAssociation?obj.GetAssociaton(context.Set <TObject>()) : context.Set <TObject>()).ToList()); } catch (Exception ex) { throw new Exception("Failed To Get Models " + typeof(TObject), ex); } } }
public static TObject GetModel <TObject>(Expression <Func <TObject, bool> > linq) where TObject : class, IEntityObject <TObject>, new() { using (var context = new ModelsDbContext()) { try { var obj = new TObject(); return((obj.HasAssociation ? obj.GetAssociaton(context.Set <TObject>()) : context.Set <TObject>()).FirstOrDefault(linq)); } catch (Exception ex) { throw new Exception("Failed To Get Model " + typeof(TObject), ex); } } }
public static List <TObject> GetModels <TObject>(Expression <Func <TObject, bool> > linq, int from, int count) where TObject : class, IEntityObject <TObject>, new() { using (var context = new ModelsDbContext()) { try { var obj = new TObject(); return((obj.HasAssociation ? obj.GetAssociaton(context.Set <TObject>()) : context.Set <TObject>()).Where(linq).Skip(from).Take(count).ToList()); } catch (Exception ex) { throw new Exception("Failed To Get Models " + typeof(TObject), ex); } } }
public static void DeleteModel <TObject>(TObject model /*, bool saveAssociations=true*/) where TObject : class, IEntityObject <TObject> { using (var context = new ModelsDbContext()) { try { model.DropAssociations(); context.Entry(model).State = EntityState.Deleted; context.SaveChanges(); } catch (Exception ex) { throw new Exception("Failed To Save Model " + typeof(TObject), ex); } } }
public static void SaveModel <TObject>(TObject model /*, bool saveAssociations=true*/) where TObject : class, IEntityObject <TObject> { using (var context = new ModelsDbContext()) { try { model.DropAssociations(); EntityState state = EntityState.Unchanged; state = context.Set <TObject>().Any(model.ObjectGuid()) ? EntityState.Modified : EntityState.Added; context.Entry(model).State = state; /*if (saveAssociations) * { * * }*/ context.SaveChanges(); context.Entry(model).Reload(); } catch (Exception ex) { throw new Exception("Failed To Save Model " + typeof(TObject), ex); } } }