public static void AddBookmarks(string username, string title, string url, string[] tags, string notes) { var dbCon = new BookmarksEntities(); using (dbCon) { var bookmark = new Bookmark(); bookmark.Notes = notes; bookmark.URL = url; bookmark.Title = title; bookmark.User = CreateOrLoadUser(dbCon, username); foreach (var tagName in tags) { var tag = CreateOrLoadTag(dbCon, tagName); bookmark.Tags.Add(tag); } var titleTags = Regex.Split(title, @"[ ,-\.;!?]+"); foreach (var titleTag in titleTags) { var tag = CreateOrLoadTag(dbCon, titleTag); bookmark.Tags.Add(tag); } dbCon.Bookmarks.Add(bookmark); dbCon.SaveChanges(); } }
private static User CreateOrLoadUser(BookmarksEntities context, string username) { var existingUser = (from u in context.Users where u.Username.ToLower() == username.ToLower() select u).FirstOrDefault(); if (existingUser != null) { return existingUser; } var newUser = new User(); newUser.Username = username; context.Users.Add(newUser); context.SaveChanges(); return newUser; }
private static Tag CreateOrLoadTag(BookmarksEntities context, string tagName) { var existingTag = (from t in context.Tags where t.Name.ToLower() == tagName.ToLower() select t).FirstOrDefault(); if (existingTag != null) { return existingTag; } var newTag = new Tag(); newTag.Name = tagName.ToLower(); context.Tags.Add(newTag); context.SaveChanges(); return newTag; }
private static void InsertBookmarks(int count) { BookmarksEntities context = new BookmarksEntities(); var userIDs = context.Users.Select(u => u.UserId).ToList(); for (int i = 0; i < count; i++) { Bookmark bookmark = new Bookmark(); bookmark.Title = "Title" + i; bookmark.URL = "http://telerikacademy.com/bookmarks/" + i; bookmark.UserId = userIDs[i % userIDs.Count]; context.Bookmarks.Add(bookmark); } context.SaveChanges(); }
private static Tag CreateOrLoadTag(BookmarksEntities context, string tagName) { var tag = context.Tags.FirstOrDefault(x => x.TagName == tagName); if (tag != null) { return(tag); } var newTag = new Tag() { TagName = tagName }; context.Tags.Add(newTag); context.SaveChanges(); return(newTag); }
private static User CreateOrLoadUser(BookmarksEntities context, string username) { var user = context.Users.FirstOrDefault(x => x.Username == username); if (user != null) { return(user); } var newUser = new User() { Username = username }; context.Users.Add(newUser); context.SaveChanges(); return(newUser); }
public static void InsertData(string username, string title, string url, string[] tags, string notes) { using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead })) { var context = new BookmarksEntities(); User user = CreateOrLoadUser(context, username); var bookmark = new Bookmark { User = user, Title = title, Notes = notes, Url = url }; foreach (var tagName in tags) { var tag = CreateOrLoadTag(context, tagName); bookmark.Tags.Add(tag); } context.Bookmarks.Add(bookmark); context.SaveChanges(); } }