예제 #1
0
        public SaveResult Delete(object dtokey)
        {
            using (NosCoreContext context = DataAccessHelper.Instance.CreateContext())
            {
                DbSet <TEntity> dbset = context.Set <TEntity>();

                if (dtokey is IEnumerable)
                {
                    foreach (object key in dtokey as IEnumerable)
                    {
                        TEntity entityfound = dbset.Find(key);

                        if (entityfound != null)
                        {
                            dbset.Remove(entityfound);
                            context.SaveChanges();
                        }
                    }
                }
                else
                {
                    TEntity entityfound = dbset.Find(dtokey);

                    if (entityfound != null)
                    {
                        dbset.Remove(entityfound);
                    }
                }
                context.SaveChanges();

                return(SaveResult.Saved);
            }
        }
예제 #2
0
 public IEnumerable <TDTO> LoadAll()
 {
     using (NosCoreContext context = DataAccessHelper.Instance.CreateContext())
     {
         DbSet <TEntity> dbset = context.Set <TEntity>();
         foreach (TEntity t in dbset)
         {
             yield return(Mapper.Map <TDTO>(t));
         }
     }
 }
예제 #3
0
        public SaveResult InsertOrUpdate(IEnumerable <TDTO> dtos)
        {
            try
            {
                using (NosCoreContext context = DataAccessHelper.Instance.CreateContext())
                {
                    context.ChangeTracker.AutoDetectChangesEnabled = false;

                    DbSet <TEntity> dbset       = context.Set <TEntity>();
                    List <TEntity>  entitytoadd = new List <TEntity>();
                    foreach (TDTO dto in dtos)
                    {
                        TEntity entity = Mapper.Map <TEntity>(dto);
                        object  value  = PrimaryKey.GetValue(dto, null);

                        TEntity entityfound = null;
                        if (value is object[])
                        {
                            entityfound = dbset.Find((object[])value);
                        }
                        else
                        {
                            entityfound = dbset.Find(value);
                        }

                        if (entityfound != null)
                        {
                            Mapper.Map(entity, entityfound);

                            context.Entry(entityfound).CurrentValues.SetValues(entity);
                        }

                        if (value == null || entityfound == null)
                        {
                            //add in a temp list in order to avoid find(default(PK)) to find this element before savechanges
                            entitytoadd.Add(entity);
                        }
                    }

                    dbset.AddRange(entitytoadd);
                    context.ChangeTracker.AutoDetectChangesEnabled = true;
                    context.SaveChanges();
                    return(SaveResult.Saved);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return(SaveResult.Error);
            }
        }
예제 #4
0
 public TDTO FirstOrDefault(Expression <Func <TEntity, bool> > predicate)
 {
     try
     {
         using (NosCoreContext context = DataAccessHelper.Instance.CreateContext())
         {
             DbSet <TEntity>       dbset    = context.Set <TEntity>();
             IEnumerable <TEntity> entities = Enumerable.Empty <TEntity>();
             TEntity ent = dbset.FirstOrDefault(predicate);
             return(Mapper.Map <TDTO>(ent));
         }
     }
     catch (Exception e)
     {
         Logger.Error(e);
         return(default(TDTO));
     }
 }
예제 #5
0
        public SaveResult InsertOrUpdate(ref TDTO dto)
        {
            try
            {
                using (NosCoreContext context = DataAccessHelper.Instance.CreateContext())
                {
                    TEntity         entity = Mapper.Map <TEntity>(dto);
                    DbSet <TEntity> dbset  = context.Set <TEntity>();

                    object  value       = PrimaryKey.GetValue(dto, null);
                    TEntity entityfound = null;
                    if (value is object[])
                    {
                        entityfound = dbset.Find((object[])value);
                    }
                    else
                    {
                        entityfound = dbset.Find(value);
                    }
                    if (entityfound != null)
                    {
                        Mapper.Map(entity, entityfound);

                        context.Entry(entityfound).CurrentValues.SetValues(entity);
                        context.SaveChanges();
                    }
                    if (value == null || entityfound == null)
                    {
                        dbset.Add(entity);
                    }
                    context.SaveChanges();
                    dto = Mapper.Map <TDTO>(entity);

                    return(SaveResult.Saved);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return(SaveResult.Error);
            }
        }
예제 #6
0
 public bool Initialize(SqlConnectionStringBuilder Database)
 {
     _conn = Database;
     using (NosCoreContext context = CreateContext())
     {
         try
         {
             context.Database.Migrate();
             context.Database.GetDbConnection().Open();
             Logger.Log.Info(LogLanguage.Instance.GetMessageFromKey("DATABASE_INITIALIZED"));
         }
         catch (Exception ex)
         {
             Logger.Log.Error("Database Error", ex);
             Logger.Log.Error(LogLanguage.Instance.GetMessageFromKey("DATABASE_NOT_UPTODATE"));
             return(false);
         }
         return(true);
     }
 }
예제 #7
0
 public IEnumerable <TDTO> Where(Expression <Func <TEntity, bool> > predicate)
 {
     using (NosCoreContext context = DataAccessHelper.Instance.CreateContext())
     {
         DbSet <TEntity>       dbset    = context.Set <TEntity>();
         IEnumerable <TEntity> entities = Enumerable.Empty <TEntity>();
         try
         {
             entities = dbset.Where(predicate).ToList();
         }
         catch (Exception e)
         {
             Logger.Error(e);
         }
         foreach (TEntity t in entities)
         {
             yield return(Mapper.Map <TDTO>(t));
         }
     }
 }