Пример #1
0
        public async Task <int> CommitAsync()
        {
            return(await Task.Factory.StartNew(() =>
            {
                ValidModel();
                try
                {
                    using (var trans = DbSession.Default.BeginTransaction())
                    //using(DbBatch batch = DbSession.Default.BeginBatchConnection(20))
                    {
                        if (InsertList != null && InsertList.Any())
                        {
                            // List<T> InsertNewList = InsertList.Distinct(new PropertyComparer<T>("Serialnum")).ToList();//按照特定规则排除重复项
                            trans.Insert(InsertList);
                            //batch.Insert(InsertNewList.ToArray());
                        }

                        if (DeleteList != null && DeleteList.Any())
                        {
                            trans.Delete(DeleteList);
                        }
                        //DeleteList.ForEach(delete =>
                        //{
                        //    batch.Delete(delete);
                        //});

                        if (UpdateList != null && UpdateList.Any())
                        {
                            //List<T> UpdateNewList = UpdateList.Distinct(new PropertyComparer<T>("Serialnum")).ToList();//按照特定规则排除重复项
                            int i = trans.UpdateAll <T>(UpdateList);
                            //batch.UpdateAll(UpdateNewList.ToArray());
                        }
                        trans.Commit();
                        //trans.Commit();
                        if (InsertList != null && InsertList.Any())
                        {
                            InsertList.Clear();
                        }
                        if (DeleteList != null && DeleteList.Any())
                        {
                            DeleteList.Clear();
                        }
                        if (UpdateList != null && UpdateList.Any())
                        {
                            UpdateList.Clear();
                        }
                        return 1;
                    }
                }
                catch (Exception ex)
                {
                    return 0;
                    //throw;
                }
            }));
        }
Пример #2
0
        public async Task <int> CommitAsync()
        {
            ValidModel();

            using (var scope = AhnqIotContainer.Container.BeginLifetimeScope())
            {
                var context       = scope.Resolve <IDbContext>(new NamedParameter("connectionString", Connectstring));
                var loggerFactory = ((DbContext)context).GetService <ILoggerFactory>();
                loggerFactory.AddProvider(new DbLoggerProvider(Console.WriteLine));
                var dbset = context.Set <T>();
                if (InsertList != null && InsertList.Any())
                {
                    List <T> InsertNewList = InsertList.Distinct(new PropertyComparer <T>("Serialnum")).ToList();//按照特定规则排除重复项
                    dbset.AddRange(InsertNewList);
                }

                if (DeleteList != null && DeleteList.Any())
                {
                    dbset.RemoveRange(DeleteList);
                }
                //try
                //{
                if (UpdateList != null && UpdateList.Any())
                {
                    List <T> UpdateNewList = UpdateList.Distinct(new PropertyComparer <T>("Serialnum")).ToList();//按照特定规则排除重复项
                    dbset.UpdateRange(UpdateNewList);
                }
                var result = await context.SaveChangesAsync();

                //return result;
                //}
                //catch (Exception ex)
                //{
                //    Console.Clear();
                //    Console.WriteLine(ex.ToString());
                //    throw;
                //}

                if (InsertList != null && InsertList.Any())
                {
                    InsertList.Clear();
                }
                if (DeleteList != null && DeleteList.Any())
                {
                    DeleteList.Clear();
                }
                if (UpdateList != null && UpdateList.Any())
                {
                    UpdateList.Clear();
                }
                return(result);
            }
        }
Пример #3
0
        public int Commit()
        {
            ValidModel();
            try
            {
                using (var trans = DbSession.Default.BeginTransaction())
                {
                    if (InsertList != null && InsertList.Any())
                    {
                        //List<T> InsertNewList = InsertList.Distinct(new PropertyComparer<T>("Serialnum")).ToList();//按照特定规则排除重复项
                        trans.Insert(InsertList);
                    }

                    if (DeleteList != null && DeleteList.Any())
                    {
                        trans.Delete(DeleteList);
                    }
                    if (UpdateList != null && UpdateList.Any())
                    {
                        // List<T> UpdateNewList = UpdateList.Distinct(new PropertyComparer<T>("Serialnum")).ToList();//按照特定规则排除重复项
                        trans.Update(UpdateList);
                    }

                    trans.Commit();
                    if (InsertList != null && InsertList.Any())
                    {
                        InsertList.Clear();
                    }
                    if (DeleteList != null && DeleteList.Any())
                    {
                        DeleteList.Clear();
                    }
                    if (UpdateList != null && UpdateList.Any())
                    {
                        UpdateList.Clear();
                    }
                    return(1);
                }
            }
            catch (Exception ex)
            {
                return(0);
                //throw;
            }
        }