SaveChanges() public méthode

public SaveChanges ( ) : int
Résultat int
Exemple #1
0
        public static void CreateAndAddTags(T_Article article, List<T_Tag> tags)
        {
            try
            {
                using (Entities bdd = new Entities())
                {
                    using (TransactionScope scope = new TransactionScope())
                    {

                        bdd.T_Article.Add(article);
                        bdd.SaveChanges();

                        foreach (T_Tag tag in tags)
                        {
                            T_Tag oldTag = bdd.T_Tag.Where(tg => tg.Name == tag.Name).FirstOrDefault();
                            if (oldTag == null)
                            {
                                bdd.T_Tag.Add(tag);
                                bdd.SaveChanges();
                                T_ArticleTag at = new T_ArticleTag()
                                {
                                    ArticleId = article.Id,
                                    TagId = tag.Id
                                };
                                bdd.T_ArticleTag.Add(at);
                                bdd.SaveChanges();
                            }
                            else
                            {
                                T_ArticleTag at = new T_ArticleTag()
                                {
                                    ArticleId = article.Id,
                                    TagId = oldTag.Id
                                };
                                bdd.T_ArticleTag.Add(at);
                                bdd.SaveChanges();
                            }
                        }

                        scope.Complete();
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message);
                throw;
            }
        }
        /// <summary>
        /// Inserts specified media into the database, or updates an existing entry.
        /// </summary>
        /// <param name="item">The media item to merge into the database.</param>
        /// <param name="context">The database context.</param>
        public static void InsertOrMerge(Media item, Entities context) {
            // Try to find matching element already within the database.
            List<Media> DbMatch = (from m in context.Media
                                         where m.MediaId == item.MediaId || (m.Artist == item.Artist && m.Title == item.Title)
                                         select m).ToList();
            Media DbItem;
            if (DbMatch.Count > 1) // If both matches happen simultaneously, Artist/Title must take priority to avoid creating duplicate.
                DbItem = DbMatch.Single(m => m.Artist == item.Artist && m.Title == item.Title);
            else
                DbItem = DbMatch.FirstOrDefault();

            // If merging, delete existing entry and we'll create a new one and merge the fields.
            if (DbItem != null)
                context.Media.Remove(DbItem);

            // Insert new data.
            context.Media.Add(item);

            // Merge fields.
            if (DbItem != null) {
                item.FileName = DbItem.FileName;
                item.MediaId = DbItem.MediaId; // If matching by Artist/Title, we keep the same ID to avoid creating duplicate ID.
            }

            // Commit changes.
            context.SaveChanges();
        }
 public static void UpdateVersionInfo(Version version) {
     using (Entities context = new Entities()) {
         DbVersion Row = context.DbVersions.Single();
         Row.Major = version.Major;
         Row.Minor = version.Minor;
         Row.Build = version.Build;
         Row.Revision = version.Revision;
         context.SaveChanges();
     }
 }
Exemple #4
0
 public static void Create(T_Theme theme)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             bdd.T_Theme.Add(theme);
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #5
0
 public static void Delete(long themeId)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             bdd.T_Theme.Remove(Get(themeId));
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #6
0
 public static void Delete(long id)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             bdd.T_Follow.Remove(bdd.T_Follow.Where(x => x.Id == id).FirstOrDefault());
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #7
0
 //public static void Update(long commentId, Dbo.Comment cmmnt)
 //{
 //    try
 //    {
 //        using (Entities bdd = new Entities())
 //        {
 //            T_Comment comment = Get(commentId);
 //            comment = cmmnt;
 //            bdd.SaveChanges();
 //        }
 //    }
 //    catch (Exception e)
 //    {
 //        Trace.WriteLine(e.Message);
 //        throw;
 //    }
 //}
 public static void Delete(long commentId)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             T_Comment comment = bdd.T_Comment.Where(c => c.Id == commentId).FirstOrDefault();
             if (comment == null)
                 return;
             bdd.T_Comment.Remove(comment);
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #8
0
 public static void Create(Dbo.Comment comment)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             bdd.T_Comment.Add(new T_Comment()
                 {
                     UserId = comment.UserId,
                     ArticleId = comment.ArticleId,
                     Comment = comment.Content,
                     CreationDate = comment.CreationDate
                 });
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #9
0
 public static void Update(long tagId, T_Tag tg)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             T_Tag tag = Get(tagId);
             tag = tg;
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
 /// <summary>
 /// Saves updated preference field.
 /// </summary>
 /// <param name="video">The media object to update.</param>
 public void UpdatePreference(Media item) {
     if (item.MediaId != Guid.Empty) {
         using (var context = new Entities()) {
             Media Item = context.Media.Where(v => v.MediaId == item.MediaId).Single();
             Item.Preference = item.Preference;
             Item.IsCustomPreference = (item.Preference.HasValue);
             context.SaveChanges();
         }
     }
 }
        /// <summary>
        /// Loops through the Media table to set the FileName, Length, Width and Height fields.
        /// </summary>
        /// <param name="progress">Reports the progress of the operation. First report is the amount of files to process, then subsequent reports represent the quantity done.</param>
        /// <returns>Whether some data was modified.</returns>
        public async Task<bool> LoadMediaInfoAsync(IProgress<int> progress) {
            // Calling this method when it is already running allows listening to the progress.
            if (progress != null) {
                loadingMediaInfoProgress = progress;
                // First progress report is total count.
                if (isLoadingMediaInfo)
                    loadingMediaInfoProgress.Report(loadingMediaInfoCount);
            }

            if (isLoadingMediaInfo)
                return false;
            isLoadingMediaInfo = true;

            bool HasChanges = false;
            using (Entities context = new Entities()) {
                // Loop through all media items with missing Length, Width or Height.
                var Query = (from v in context.Media
                             where v.FileName == null || v.Length == null ||
                                ((v.MediaTypeId == (int)MediaType.Video || v.MediaTypeId == (int)MediaType.Image) && v.Height == null)
                             select v);

                // First progress report contains the total count. Subsequent reports contain the quantity completed.
                loadingMediaInfoCount = Query.Count();
                if (loadingMediaInfoProgress != null)
                    loadingMediaInfoProgress.Report(loadingMediaInfoCount);

                int ItemsCompleted = 0;
                string DefaultFileName;
                string FilePath;
                DefaultMediaPath PathCalc = new DefaultMediaPath();
                PathCalc.LoadData();
                MediaInfoReader MediaInfo = new MediaInfoReader();

                foreach (Media item in Query) {
                    // Try to auto-attach file if default file name exists.
                    if (item.FileName == null) {
                        DefaultFileName = PathCalc.GetDefaultFileName(item.Artist, item.Title, item.MediaCategoryId, item.MediaType);
                        foreach (string ext in Settings.GetMediaTypeExtensions(item.MediaType)) {
                            FilePath = Settings.NaturalGroundingFolder + DefaultFileName + ext;
                            if (File.Exists(FilePath)) {
                                item.FileName = DefaultFileName + ext;
                                HasChanges = true;
                                await Task.Delay(1);
                                break;
                            }
                        }
                    }

                    // Load media file to set Length, Width and Height.
                    if (item.FileName != null && await MediaInfo.LoadInfoAsync(item))
                        HasChanges = true;

                    // Send update with the quantity of files completed.
                    if (loadingMediaInfoProgress != null)
                        loadingMediaInfoProgress.Report(++ItemsCompleted);
                }
                MediaInfo.Dispose();
                if (HasChanges)
                    context.SaveChanges();
            }
            isLoadingMediaInfo = false;
            loadingMediaInfoCount = 0;
            loadingMediaInfoProgress = null;
            return HasChanges;
        }
Exemple #12
0
 public static void Update(long blogId, T_Blog blg)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             T_Blog blog = Get(blogId);
             blog = blg;
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #13
0
 public static void UpdateStyle(string username, long styleId)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             T_User user = bdd.T_User.Where(x => x.Login == username).ToList().FirstOrDefault();
             user.StyleId = styleId;
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #14
0
 public static void Update(long userId, T_User usr)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             T_User user = Get(userId);
             user = usr;
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #15
0
 public static void Delete(long articleId)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             T_Article article = bdd.T_Article.Where(art => art.Id == articleId).FirstOrDefault();
             if (article == null)
                 return;
             bdd.T_ArticleTag.Where(aTag => aTag.ArticleId == articleId).ToList().ForEach(aTag => bdd.T_ArticleTag.Remove(aTag));
             bdd.T_Article.Remove(article);
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #16
0
 public static void Update(long followId, T_Follow fllw)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             T_Follow follow = Get(followId);
             follow = fllw;
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #17
0
 public static void Update(long articleId, T_Article newT_Article)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             T_Article article = Get(articleId);
             article = newT_Article;
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #18
0
 public static void Update(long themeId, T_Theme thm)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             T_Theme theme = Get(themeId);
             theme = thm;
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #19
0
 public static void Update(long styleId, T_Style stl)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             T_Style style = Get(styleId);
             style = stl;
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }
Exemple #20
0
 public static void Update(long mediaTypeId, T_MediaType mdType)
 {
     try
     {
         using (Entities bdd = new Entities())
         {
             T_MediaType mediaType = Get(mediaTypeId);
             mediaType = mdType;
             bdd.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine(e.Message);
         throw;
     }
 }