/// <summary> /// Obtiene un listado de de albums segun el id y titulo del album asi como el perfil /// </summary> /// <param name="AlbumId">id del album</param> /// <param name="Titulo">Titulo del album</param> /// <param name="PerfilId">Id perfil</param> /// <history> /// [egongora] Created /// [ecanul] 03/02/2017 Modified. La cosulta la hago desde EF porque no me pasaste el sp /// </history> public static List <Album> Get(int AlbumId, int PerfilId, string Titulo = "") { try { PerfilId = 3; var item = new List <Album>(); using (var db = new Entities(ConnectionStringHelper.ConnectionString())) { item = db.st_SelAlbum_(AlbumId, Titulo, PerfilId).Select(x => new Album { AlbumId = x.AlbumId, Titulo = x.Titulo, Imagen = x.Imagen, FechaPublicacionTexto = x.FechaPublicacion.ToString("dd/mm/yyyy"), Formato = x.Formato, Contenido = x.Contenido, Precio = x.Precio, Oferta = x.Oferta, LinkCompra = x.LinkCompra, Promocion = x.Promocion, PerfilId = x.PerfilId, Estatus = x.Estatus, UrlAlbumPrecargado = x.UrlAlbumPrecargado, UsarAlbumPrecargado = x.UsarAlbumPrecargado, SubGenero = new SubGenero { SubGeneroId = x.SubGeneroId, Descripcion = x.SubGenero }, LTag = new List <Tag>() //AlbumTag = AlbumTagDAO.ListByAlbum(x.AlbumId) //LTag = new List<Tag>(TagDAO.GetTagsByAlbum(AlbumId).ToList()) // users = m.users.Where(u => u.surname == "surname").ToList() }).ToList(); } //return (from a in IDs // from b in a.Values // where b.Code == code // select (new A { ID = a.ID, Values = new List<B> // { new B { Code = b.Code, DisplayName = b.DisplayName } } // })).FirstOrDefault(); var tags = TagDAO.GetTagsByAlbum(item[0].AlbumId); foreach (var tag in tags) { item[0].LTag.Add(new Tag { TagId = tag.TagId, Nombre = tag.Nombre }); } //item[0].LTag = return(item); } catch (EntityException ex) { //Devuelve el valor arrojado por cliente de entity framework (Es mas detallado que el error que obtiene .net por si solo throw ex; } }
/// <summary> /// Guarda un elemento nuevo o ya existente en la base de datos /// </summary> /// <param name="item">Album a guardar</param> /// <history> /// [egongora] created /// </history> public static Album Save(Album item, List <Tag> Ltag = null) { TransactionOptions options = new TransactionOptions(); options.IsolationLevel = IsolationLevel.ReadCommitted; options.Timeout = new TimeSpan(0, 5, 0); try { using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options)) { ObjectParameter AlbumId = new ObjectParameter("AlbumId", item.AlbumId); using (var db = new Entities(ConnectionStringHelper.ConnectionString())) { try { //Se inserta la informacion var i = db.st_InsAlbum_(item.AlbumId, item.Titulo, item.Imagen, item.FechaPublicacion, item.Formato, item.Contenido, item.Precio, item.Oferta, item.LinkCompra, item.Promocion, item.PerfilId, item.SubGenero.SubGeneroId, item.Estatus, item.FechaRegistro, item.UrlAlbumPrecargado, item.UsarAlbumPrecargado); //Se obtiene el album recien ingresado int albumId = (item.AlbumId > 0) ? item.AlbumId : Convert.ToInt32(AlbumId.Value); var album = db.Album.Where(x => x.Titulo == item.Titulo && x.PerfilId == item.PerfilId).FirstOrDefault(); //Eliminamos los tags que tenga asignados if (item.LTag != null && item.LTag.Count > 0) { //Eliminamos todos los tags del album AlbumTagDAO.Delete(new AlbumTag { AlbumId = albumId }); //Llenamos los tags del album foreach (Tag tag in item.LTag) { //Obtenemos el tag var t = TagDAO.Get(tag); //si el tag no existe se crea if (t == null) { t = TagDAO.Save(tag); } //Guardamos la relacion AlbumTagDAO.Save(new AlbumTag { AlbumId = album.AlbumId, TagId = t.TagId }); } } scope.Complete(); return(album); } catch (EntityException ex) { scope.Dispose(); throw ex; } } } } catch (EntityException ex) { throw ex; } }
public static void Save(Evento item) { // si necesitamos que un procedure devuelva el valor de un id hacemos esto //https://social.msdn.microsoft.com/Forums/en-US/5e56547d-75f0-4688-8069-8328de24f332/error-when-calling-a-stored-procedure?forum=adodotnetentityframework // si no sabemos como activar la ventana del link de arriba checamos esto //http://stackoverflow.com/questions/3729920/cant-find-ado-net-entity-model-browser-window-in-vs2010 TransactionOptions options = new TransactionOptions(); options.IsolationLevel = IsolationLevel.ReadCommitted; options.Timeout = new TimeSpan(0, 5, 0); try { using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options)) { System.Data.Entity.Core.Objects.ObjectParameter EventoId = new System.Data.Entity.Core.Objects.ObjectParameter("EventoId", item.EventoId); using (var db = new Entities(ConnectionStringHelper.ConnectionString())) { try { db.st_InsEvento(EventoId, item.Titulo, item.Imagen, item.FechaEvento, item.Direccion, item.Establecimiento, item.PrecioRegular, item.Promocion, item.Preventa, item.EventoTipo.EventoTipoId, item.Ciudad.CiudadId, item.Latitud, item.Longitud, item.LinkEventoFacebook, item.LinkComprarBoleto, item.Estatus, item.Perfil.PerfilId ); int eventoid = (item.EventoId > 0) ? item.EventoId : Convert.ToInt32(EventoId.Value); //TRABAJADMOS CON EVENTOPERFIL //eliminamos los tags de EventoPerfil que tenga asignado if (item.lPerfil != null) { if (item.lPerfil.Count > 0) { //eliminamos las bandas que este evento tenga asignado db.st_DelEventoPerfil(eventoid, 3); foreach (Perfil EP in item.lPerfil) { // validamos si la banda existe en la lista de perfil var banda = db.Perfil.Where(x => x.Nombre == EP.Nombre && x.PerfilTipoId == 2).FirstOrDefault(); // si existe entonces insertamos normalmente if (banda != null) { // podemos corregir en el sp que no tome el nombre si no que del resultado de linq //asignamos el id (banda.perfilId) db.st_InsEventoPerfil(eventoid, EP.Nombre); } //si no existe significa que burlaron el jquery y no debemos insertar. } } } // TRABAJAMOS CON EVENTOTAG if (item.lTag != null) { if (item.lTag.Count > 0) { //eliminamos los tags que este evento tenga asignado db.st_DelEventoTag(eventoid, 3); foreach (Tag ET in item.lTag) { // validamos si el tag existe en la lista de tags var tag = db.Tag.Where(x => x.Nombre == ET.Nombre).FirstOrDefault(); //si no existe entonces significa que es un tag nuevo lo insertamos en la tabla tags if (tag == null) { TagDAO.Save(ET); //System.Data.Entity.Core.Objects.ObjectParameter TagId = new System.Data.Entity.Core.Objects.ObjectParameter("TagId", ET.TagId); //db.st_InsEventoTag(eventoid, ET.Nombre); } db.st_InsEventoTag(eventoid, ET.Nombre); //// si existe entonces significa que el tag existe y solo insertamos la relacion //else //{ // //System.Data.Entity.Core.Objects.ObjectParameter TagId = new System.Data.Entity.Core.Objects.ObjectParameter("TagId", ET.TagId); // db.st_InsEventoTag(eventoid, ET.Nombre); //} } } } // TRABAJAMOS CON EVENTOVIDEO if (item.lEventoVideo != null) { if (item.lEventoVideo.Count > 0) { //eliminamos los videos que este evento tenga asignado db.st_DelEventoVideo(eventoid, 3); foreach (EventoVideo EV in item.lEventoVideo) { if (!String.IsNullOrWhiteSpace(EV.UrlVideo)) { string urlformato = getUrlEmbed(EV.UrlVideo); if (!String.IsNullOrWhiteSpace(urlformato)) { db.st_InsEventoVideo(eventoid, urlformato); } } } } } scope.Complete(); } catch (Exception ex) { scope.Dispose(); throw ex; } } } } catch (Exception ex) { } }