Mostrando entradas con la etiqueta entity. Mostrar todas las entradas
Mostrando entradas con la etiqueta entity. Mostrar todas las entradas

viernes, 4 de agosto de 2017

C# - Obtener el máximo número (entity)

//previamente la tabla (con su respectiva PRIMARY KEY) debe estar incluida en el MODELO.


 private int NewCodJuzgado()
        {
            var newId = 0;
            using (var context = new leaOperModel())
            {
                newId = context.p_juzgados.Select(p => p.cod_juzgado).Max() + 1;
            }
            return newId;
        }

jueves, 21 de abril de 2016

C# - Ejemplo de CRUD Entity Framework

  #region Metodos CRUD Publicos

        /// <summary>
        /// Metodo Select
        /// </summary>
        /// /// <param name="numProyecto"></param>
        /// <param name="msj"></param>
        /// <returns></returns>
        public List<RequerimientoBe> Select(Int32 numProyecto,ref string msj)
        {
            var obj = new List<RequerimientoBe>();
            try
            {
                using (var context = new WorkflowComexEntities())
                {
                    var dtContex = context.REQUERIMIENTO.Where(p => p.ID_PROYECTO == numProyecto).ToList();
                    obj.AddRange(dtContex.Select(RequerimientoBe.DbToBe));
                }
            }
            catch (Exception ex)
            {
                msj = ex.Message;
            }
            return obj.ToList();
        }

        /// <summary>
        /// Metodo Insert
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="msj"></param>
        public void Insert(RequerimientoBe obj, ref string msj)
        {
            var newId = 0;
            try
            {
                using (var context = new WorkflowComexEntities())
                {
                    if (obj != null)
                    {
                        var item = RequerimientoBe.BeToDb(ref obj);
                        context.REQUERIMIENTO.AddObject(item);
                        context.SaveChanges();
                        newId = context.REQUERIMIENTO.Max(p => p.ID_REQUERIMIENTO);
                    }
                }
            }
            catch (Exception ex)
            {
                msj = ex.Message;
            }
        }

        /// <summary>
        /// Metodo Update
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="msj"></param>
        public void Update(RequerimientoBe obj, ref string msj)
        {
            try
            {
                using (var context = new WorkflowComexEntities())
                {
                    if (obj != null)
                    {
                        var item = RequerimientoBe.BeToDb(ref obj);
                        object originalObject = null;
                        var key = context.CreateEntityKey("REQUERIMIENTO", item);
                        if (!context.TryGetObjectByKey(key, out originalObject)) return;

                        context.REQUERIMIENTO.ApplyCurrentValues(item);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                msj = ex.Message;
            }

        }

        /// <summary>
        /// Metodo Delete
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="msj"></param>
        public void Delete(RequerimientoBe obj, ref string msj)
        {
            try
            {
                using (var context = new WorkflowComexEntities())
                {
                    if (obj != null)
                    {
                        var item = context.REQUERIMIENTO.FirstOrDefault(p => p.ID_REQUERIMIENTO == obj.ID_REQUERIMIENTO);
                        context.REQUERIMIENTO.DeleteObject(item);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                msj = ex.InnerException != null ? ex.InnerException.Message : ex.Message;

            }
        }
        #endregion

miércoles, 30 de marzo de 2016

C# - Tabla Temporal en SP y Entity Framework

SET FMTONLY OFF     : Para que el entity framework reconozca las columnas del SP, cuando hay tablas temporales (# , @) involucradas.


CREATE PROCEDURE [dbo].[spca_consultas_formalizacion_hoja_ruta_wf_operativo_by_num_docum]
@num_docum numeric(20, 0)
AS

/***************************************************************************************
JC 29-03-2016
Obtiene operaciones y documentos
llamada desde: D:\SFSLeasingSantander\LeaseOperWeb\LeaseOperWeb\Paginas\Compras\Workflow\AdmHojaRuta.aspx

spca_consultas_formalizacion_hoja_ruta_wf_operativo_by_num_docum 123544
***************************************************************************************/

BEGIN
SET FMTONLY OFF    


--instrucciones del sp

END

miércoles, 20 de enero de 2016

C# - retorno de DataSet desde consulta al Entity Framework / Convertir lista a DataTable

  public DataSet SelectbyPeriodoExcel(int periodo, ref string msj)
        {
            var ds = new DataSet();
            DataTable dt = new DataTable();
            try
            {
                using (var context = new leaOperModel())
                {
                    var dtContex = context.t_c13.Where(p => p.periodo == periodo).ToList();
                    // obj.AddRange(dtContex.Select(C13Be.DbToBe));
                    dt = ConvertToDataTable(dtContex);
                    ds.Tables.Add(dt);
                }
            }
            catch (Exception ex)
            {
                msj = ex.Message;
            }
            return ds;
            //return obj.ToList();
        }



 public DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
               TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;

        }