public user_share_file addFiles() { var newFile = new file() { name = "test222", type = 1, url = "test222", }; using (FileEntitiesFinal entities = new FileEntitiesFinal()) { entities.Entry <file>(newFile).State = System.Data.Entity.EntityState.Added; entities.SaveChanges(); entities.Entry(newFile); user_share_file newShare = new user_share_file() { user_id = 2, file_id = newFile.id, code = "1234" }; entities.user_share_file.Add(newShare); entities.SaveChanges(); return(newShare); } }
// insert new use transaction public async Task registerUser(user newUser) { using (TransactionScope scope = new TransactionScope()) { using (FileEntitiesFinal entity = new FileEntitiesFinal()) { entity.user.Add(newUser); entity.SaveChanges(); } scope.Complete(); } }
public HttpResponseMessage deleteFile(string id) { //string id = HttpContext.Current.Request.Params["fileId"].ToString(); int fileId = 0; HttpResponseMessage response = null; //response.Headers.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue() //{ // MaxAge = TimeSpan.FromMinutes(20) //}; try { fileId = Convert.ToInt32(id); } catch (Exception e) { response = Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid File"); return(response); } var userId = HttpContext.Current.Session["id"]; using (FileEntitiesFinal entity = new FileEntitiesFinal()) { var files = from f in entity.file where f.id == fileId select f; file tempFile = files.FirstOrDefault(); if (tempFile.user_id != (int)userId) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Operation deny")); } if (tempFile.status == 100) { response = Request.CreateResponse(HttpStatusCode.BadRequest, "File had been deleted, don't delete it again."); return(response); } tempFile.status = 100; entity.Entry <file>(tempFile).State = System.Data.Entity.EntityState.Modified; entity.SaveChanges(); string message = string.Format("Delete file {0} successfully!", tempFile.name.ToString()); //response.Content = new StringContent(message, Encoding.Unicode); response = Request.CreateResponse(HttpStatusCode.OK, message); return(response); } }