protected void LecturesList_RowDeleting(object sender, GridViewDeleteEventArgs e) { using (ResourceContext db = new ResourceContext()) { Lecture lecture = db.LecturesContext.Find(e.Keys[0]); db.LecturesContext.Remove(lecture); db.SaveChanges(); } Response.Redirect("Lectures"); }
public static void Build(ResourceContext resourceContext) { resourceContext.Resources.Add(new Resource { Key = "key1", Partition = "part1", Value = "value1" }); resourceContext.Resources.Add(new Resource { Key = "key2", Partition = "part2", Value = "value2" }); resourceContext.Resources.Add(new Resource { Key = "key3", Partition = "part2", Value = "value3" }); resourceContext.SaveChanges(); }
public static bool CreateNewResource(ResourceModel resource) { if (resource == null) { return(false); } using (var db = new ResourceContext()) { db.Resources.Add(resource); db.SaveChanges(); return(true); } }
public static bool DeleteResource(int id) { if (id <= 0) { return(false); } using (var db = new ResourceContext()) { var resource = db.Resources.Where(x => x.ID == id).FirstOrDefault(); db.Resources.Remove(resource); db.SaveChanges(); return(true); } }
protected void DownloadMaterial(object sender, EventArgs e) { Response.ContentType = DetailType.Text; Response.AppendHeader("Content-Disposition", "attachment; filename=" + DetailFilename.Text); Response.TransmitFile(Server.MapPath(DetailSource.Value)); Response.End(); using (ResourceContext db = new ResourceContext()) { int materialId = Convert.ToInt32(DetailId.Value); Material material = db.MaterialsContext.Find(materialId); material.DownloadTimes = material.DownloadTimes + 1; db.Entry(material).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } }
protected override void Up(MigrationBuilder migrationBuilder) { using (var db = new ResourceContext()) { // Only perform the following operation if this is an empty db. This is to // allow for powershell scripts outside to seed the system. if (db.Resources.Count() == 0) { db.Resources.Add(new Resource { Key = "BaseValueSegmentFeature", Partition = "dev", Value = "true" }); db.Resources.Add(new Resource { Key = "service.legalpartysearch", Partition = "urlservices:dev", Value = "http://" }); db.SaveChanges(); } } }
static void WriteToNewDatabase(string databaseName, ResourceCategory[] outputCategories, Resource[] outputResources) { Console.WriteLine("==Start to write to new database=="); var options = new DbContextOptionsBuilder <ResourceContext>() .UseMySql($"Server=localhost;Port=3306;Database={databaseName};Uid=root;Pwd=root;") .EnableSensitiveDataLogging() .Options; using (var context = new ResourceContext(options)) { context.Database.EnsureCreated(); foreach (var category in outputCategories) { context.ResourceCategories.Add(category); } context.SaveChanges(); } using (var context = new ResourceContext(options)) { foreach (var resource in outputResources) { context.Resources.Add(resource); } context.SaveChanges(); } Console.WriteLine("Succeed. Press any key to exit."); Console.ReadLine(); //using (var context = new dco_resourcesContext()) //{ // var binding = context.CategoryListingBindings.AsNoTracking().Take(10).ToList(); // foreach (var item in binding) // { // Console.WriteLine(item.CategoryId+"<---->"+item.ListId); // } //} }
private static void FlattenCategories(string newDatabase) { Console.WriteLine("==Start to flatten the resources =="); var optionsForNewDb = new DbContextOptionsBuilder <ResourceContext>() .UseMySql($"Server=localhost;Port=3306;Database={newDatabase};Uid=root;Pwd=root;") .EnableSensitiveDataLogging() .Options; using (var context = new ResourceContext(optionsForNewDb)) { var resources = context.Resources.Include(r => r.Category).ToList(); var categories = context.ResourceCategories.Include(c => c.ChildrenCategories) .Include(c => c.Resources) .ToList(); var tagSet = new HashSet <Tag>(); var rootCats = from c in categories where c.Depth == 0 select c; foreach (var rootCat in rootCats) { foreach (var subCat in rootCat.ChildrenCategories) { FlattenCategoriesHelper(subCat, subCat, tagSet); } } Console.WriteLine($"===Start to write to {newDatabase} ==="); using (var newContext = new ResourceContext(optionsForNewDb)) { newContext.Database.EnsureCreated(); newContext.Resources.UpdateRange(resources); newContext.ResourceCategories.UpdateRange(categories); newContext.Tags.AddRange(tagSet); newContext.SaveChanges(); } } Console.WriteLine("Flattening successful."); Console.ReadKey(); }
/// <summary> /// Adding new resource. /// </summary> /// <param name="resource">New object</param> /// <returns>Added Resource object</returns> public Result <Resource> AddNewResource(Resource resource) { if (resource == null) { throw new ArgumentNullException(); } using (var resourceContext = new ResourceContext()) { try { resourceContext.Resources.Add(resource); resourceContext.SaveChanges(); return(new Result <Resource>(resource)); } catch (Exception e) { return(new Result <Resource>(e.Message)); } } }
private static void RemoveEmptyCategories(string database) { Console.WriteLine("==Start to Remove empty categories the resources =="); var optionsForNewDb = new DbContextOptionsBuilder <ResourceContext>() .UseMySql($"Server=localhost;Port=3306;Database={database};Uid=root;Pwd=root;") .EnableSensitiveDataLogging() .Options; using (var context = new ResourceContext(optionsForNewDb)) { var secondLevelCategories = context.ResourceCategories.Include(c => c.Resources) .Where(r => r.Depth >= 2) .ToList(); var emptyCategories = from c in secondLevelCategories where c.Resources.Count == 0 select c; context.ResourceCategories.RemoveRange(emptyCategories); context.SaveChanges(); } }
protected void SaveLecture(object sender, EventArgs e) { string savePath = Server.MapPath("~/resources/" + User.Identity.GetUserId() + "/"); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } if (MaterialInput.HasFiles) { if (ModelState.IsValid) { using (ResourceContext db = new ResourceContext()) { Lecture lecture = new Lecture { Title = LectureTitle.Text, Notes = NotesTextBox.Text, DateCreated = DateTime.Now, AuthorId = User.Identity.GetUserId() }; foreach (HttpPostedFile uploadedFile in MaterialInput.PostedFiles) { uploadedFile.SaveAs(Path.Combine(savePath, uploadedFile.FileName)); Material material = new Material { Filename = uploadedFile.FileName, ContentType = uploadedFile.ContentType, ContentLength = uploadedFile.ContentLength, DateUploaded = DateTime.Now }; lecture.Materials.Add(material); } db.LecturesContext.Add(lecture); db.SaveChanges(); } } } Response.Redirect("Lectures"); }
public static bool UpdateResource(int id) { if (id <= 0) { return(false); } using (var db = new ResourceContext()) { var resource = db.Resources.Where(x => x.ID == id).FirstOrDefault(); var oldResource = db.Resources.Where(x => x.ID == resource.ID).FirstOrDefault(); if (oldResource == null) { return(false); } oldResource = resource; db.Resources.Update(oldResource); db.SaveChanges(); return(true); } }
public static void MergeDuplicateTags(ResourceContext context) { //get all unique tag names var uniqueTags = context.Tags.AsNoTracking() .GroupBy(t => t.Name) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList(); var associations = context.ResourceTags.AsNoTracking() .Include(a => a.Resource) .Include(a => a.Tag) .ToList(); foreach (var tagName in uniqueTags) { var newTag = new Tag { Name = tagName }; context.Tags.Add(newTag); context.SaveChanges(); var oldAssoc = from a in associations where a.Tag.Name == tagName select a; var newAssoc = from a in associations where a.Tag.Name == tagName select new ResourceTag { ResourceID = a.ResourceID, TagID = newTag.ID }; context.ResourceTags.RemoveRange(oldAssoc); context.ResourceTags.AddRange(newAssoc); } }
/// <summary> /// Deleting resource by Id. /// </summary> /// <param name="id">Resource Id</param> /// <returns>true</returns> public Result DeleteResourceById(int id) { using (var resourceContext = new ResourceContext()) { try { var resourceToRemove = resourceContext.Resources.FirstOrDefault(r => r.Id == id); if (resourceToRemove == null) { return(new Result("No resource to delete.")); } resourceContext.Resources.Remove(resourceToRemove); resourceContext.SaveChanges(); return(new Result(true)); } catch (Exception e) { return(new Result(e.Message)); } } }
public void Post([FromBody] Resources resources) { _context.Resources.Add(resources); _context.SaveChanges(); }
public void Create(Resource resource) { _resourceContext.Resources.Add(resource); _resourceContext.SaveChanges(); }
public void Save() { db.SaveChanges(); }