Exemple #1
0
        public string InitializeTimelinesMovilizacion()
        {
            var db = new edayRoomEntities();

            db.ClearMovilizacion();

            var centros = db.Centroes.Include("Movilizadors").Where(m => m.movilizacion).ToList();


            var tlMovilizacion   = new List <MovilizacionTimeline>();
            var movilizacionList = new List <Movilizacion>();



            foreach (var c in centros)
            {
                tlMovilizacion.Add(new MovilizacionTimeline
                {
                    activa    = true,
                    id_centro = c.id,
                    fecha     = DateTime.Now
                });
                var testigo = c.Movilizadors.FirstOrDefault(t => t.activo);
                movilizacionList.Add(new Movilizacion
                {
                    active    = true,
                    id_centro = c.id,
                    fecha     = DateTime.Now,
                    conteo    = 0,
                    id_parent = null,

                    id_movilizador =
                        testigo == null? null : (int?)testigo.id
                });
            }



            using (
                var con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnSimple"].ConnectionString)
                )
            {
                con.Open();
                #region Movilizacion Timeline
                using (var tran = con.BeginTransaction())
                {
                    var bc = new SqlBulkCopy(con,
                                             SqlBulkCopyOptions.CheckConstraints |
                                             SqlBulkCopyOptions.FireTriggers |
                                             SqlBulkCopyOptions.KeepNulls, tran)
                    {
                        BatchSize = 1000, DestinationTableName = "movilizacionTimeline"
                    };

                    bc.WriteToServer(tlMovilizacion.AsDataReader());

                    tran.Commit();
                }
                #endregion
                #region Movilizacion Items
                using (var tran = con.BeginTransaction())
                {
                    var bc = new SqlBulkCopy(con,
                                             SqlBulkCopyOptions.CheckConstraints |
                                             SqlBulkCopyOptions.FireTriggers |
                                             SqlBulkCopyOptions.KeepNulls, tran)
                    {
                        BatchSize = 1000, DestinationTableName = "movilizacion"
                    };

                    bc.WriteToServer(movilizacionList.AsDataReader());

                    tran.Commit();
                }
                #endregion
                con.Close();
            }



            return("todo inicializado, bien");
        }