/// <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) { } }