SaveChanges() 공개 메소드

public SaveChanges ( ) : void
리턴 void
예제 #1
0
        // PUT api/MyData/5
        public IHttpActionResult PutMyData(int id, MyData mydata)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != mydata.Id)
            {
                return(BadRequest());
            }

            db.Entry(mydata).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MyDataExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
예제 #2
0
        // [EnableQuery]
        public IActionResult Post([FromBody] Product product, CancellationToken token)
        {
            _context.Products.Add(product);

            _context.SaveChanges();

            return(Created(product));
        }
예제 #3
0
        public ActionResult Create([Bind(Include = "Id,Name")] Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(category));
        }
예제 #4
0
        public ActionResult Index(Persona p)
        {
            if (ModelState.IsValid)
            {
                var persona = GetPersona(p.Nombre, p.Apellido);

                if (p.Id == 0)
                {
                    ViewBag.Crud = "Registrar";

                    if (persona != null)
                    {
                        ViewBag.Message = "Ya hay una Persona con los mismos datos";
                    }
                    else
                    {
                        db.Persona.Add(p);
                        ViewBag.Message = "Se Registró Persona";
                    }
                }
                else
                {
                    ViewBag.Crud = "Modificar";

                    if (persona != null)
                    {
                        ViewBag.Message = "Ya hay una Persona con los mismos datos";
                    }
                    else
                    {
                        db.Entry(p).State = EntityState.Modified;
                        ViewBag.Message   = "Se Grabaron los cambios";
                    }
                }
                db.SaveChanges();
                ViewBag.Listado = db.Persona.ToList();
                return(View("Index", p));
            }
            else
            {
                if (p.Id == 0)
                {
                    ViewBag.Crud = "Registrar";
                }
                else
                {
                    ViewBag.Crud = "Modificar";
                }

                ViewBag.Message = "Aún no ha terminado de llenar los campos";
                ViewBag.Listado = db.Persona.ToList();
                return(View("Index", p));
            }
        }
예제 #5
0
        public ActionResult Create([Bind(Include = "Id,Name")] Vendor vendor)
        {
            if (ModelState.IsValid)
            {
                db.Vendors.Add(vendor);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(vendor));
        }
예제 #6
0
        public ActionResult Create([Bind(Include = "Id,Enum1,Enum2,FlagsEnum")] MyData mydata)
        {
            if (ModelState.IsValid)
            {
                db.MyDatas.Add(mydata);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(mydata));
        }
        public ActionResult Create([Bind(Include = "Dept_Id,Name")] Department department)
        {
            if (ModelState.IsValid)
            {
                db.Departments.Add(department);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(department));
        }
        public ActionResult Create([Bind(Include = "Admin_Id,Password")] Admin admin)
        {
            if (ModelState.IsValid)
            {
                db.Admins.Add(admin);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(admin);
        }
예제 #9
0
        public ActionResult Create([Bind(Include = "Id,Email,Password")] Login login)
        {
            if (ModelState.IsValid)
            {
                db.Logins.Add(login);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(login));
        }
예제 #10
0
        public ActionResult Create([Bind(Include = "Id,Name,Description")] Inventory inventory)
        {
            if (ModelState.IsValid)
            {
                db.Inventories.Add(inventory);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(inventory));
        }
        public ActionResult Create([Bind(Include = "Fact_Id,Name,Password,Dept_Id")] Faculty faculty)
        {
            if (ModelState.IsValid)
            {
                db.Faculties.Add(faculty);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.Dept_Id = new SelectList(db.Departments, "Dept_Id", "Name", faculty.Dept_Id);
            return(View(faculty));
        }
        public ActionResult Create([Bind(Include = "Stud_Id,Name,Password,Dept_Id,Attendance,TotalDays")] Student student)
        {
            if (ModelState.IsValid)
            {
                db.Students.Add(student);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.Dept_Id = new SelectList(db.Departments, "Dept_Id", "Name", student.Dept_Id);
            return(View(student));
        }
예제 #13
0
        public static int UpdateEntryByProperty <T>(this MyDataContext _db, T entity, string EntityKey) where T : class
        {
            DbSet <T> dbSet = _db.Set <T>();

            dbSet.Attach(entity);
            MemberInfo[]             members    = entity.GetType().GetMembers();
            IEnumerable <MemberInfo> properties = members.Where(m => m.MemberType == MemberTypes.Property && m.Name != EntityKey);

            foreach (MemberInfo mInfo in properties)
            {
                object o = entity.GetType().InvokeMember(mInfo.Name, BindingFlags.GetProperty, null, entity, null);
                if (o != null)
                {
                    if (o.GetType().IsPrimitive || o.GetType().IsPublic)
                    {
                        try
                        {
                            DbEntityEntry entry = _db.Entry <T>(entity);
                            entry.Property(mInfo.Name).IsModified = true;
                        }
                        catch
                        {
                        }
                    }
                }
            }

            return(_db.SaveChanges());
        }
예제 #14
0
        static void Main(string[] args)
        {
            using (var context = new MyDataContext())
            {
                var cat = new Category {
                    Name = "Gifts"
                };
                for (var x = 0; x < 10; x++)
                {
                    Console.WriteLine("Creating 'Product {0}'", x);

                    var prod = new Product
                    {
                        Name     = "Product " + x,
                        Price    = x * 100,
                        Category = cat      // will be created as a child record
                                            // in the same command. Scary but powerful.
                    };

                    context.Products.Add(prod);
                    context.SaveChanges();
                }
            }
            Console.ReadLine();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Personel p = new Personel
            {
                Adi    = txt1.Text,
                Soyadi = txt2.Text,
                TCKN   = txt3.Text
            };

            dataContext.Personels.Add(p);
            dataContext.SaveChanges();
            dt1.ItemsSource = dataContext.Personels.ToList();

            txt1.Text = "";
            txt2.Text = "";
            txt3.Text = "";
        }
예제 #16
0
 public void Post(Comment comment)
 {
     using (var db = new MyDataContext())
     {
         db.Entry(comment).State = comment.CommentId == 0 ?
                                   System.Data.Entity.EntityState.Added :
                                   System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
 }
예제 #17
0
 public void Get(User newUser)
 {
     using (var db = new MyDataContext())
     {
         db.Entry(newUser).State = newUser.UserId == 0 ?
                                   System.Data.Entity.EntityState.Added :
                                   System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
 }
예제 #18
0
        public ActionResult Create([Bind(Include = "Id,CategoryId,Name,Variety")] Item item, HttpPostedFileBase ItemImage)
        {
            if (ModelState.IsValid)
            {
                item.Category = db.Categories.Find(item.CategoryId);

                if (ItemImage != null)
                {
                    string newFileName = Helpers.Helper.RandomString() + ".jpg";
                    ItemImage.SaveAs(HttpContext.Server.MapPath("~/Images/") + newFileName);
                    item.ItemImage = newFileName;
                }

                db.Items.Add(item);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(item));
        }
예제 #19
0
        /// <summary>
        /// Writes the line.
        /// </summary>
        /// <param name="message">The message.</param>
        public override void WriteLine(String message)
        {
            SyncerLog log = new SyncerLog()
            {
                Guid      = Guid.NewGuid(),
                Timestamp = DateTime.Now,
                Message   = message
            };
            MyDataContext tempContext = new MyDataContext();

            tempContext.SyncerLogs.Add(log);
            tempContext.SaveChanges();
        }
예제 #20
0
        public void Delete(int commentId)
        {
            using (var db = new MyDataContext())
            {
                var commentToDelete = db.Comments.Find(commentId);

                if (commentToDelete != null)
                {
                    db.Entry(commentToDelete).State = System.Data.Entity.EntityState.Deleted;
                }

                db.SaveChanges();
            }
        }
예제 #21
0
        public ActionResult Create([Bind(Include = "Id,ItemId,VendorId")] VendorSupplyItem item)
        {
            if (ModelState.IsValid)
            {
                item.Item   = db.Items.Find(item.ItemId);
                item.Vendor = db.Vendors.Find(item.VendorId);

                if (IsExistItemVendor(item.ItemId, item.VendorId, null))
                {
                    ModelState.AddModelError("ItemId", "Already exist for Item and Vendor!");

                    ViewBag.Items = new SelectList(db.Items.ToList().Select(
                                                       x => new SelectListItem()
                    {
                        Text  = x.Name,
                        Value = x.Id.ToString()
                    }
                                                       ), "Value", "Text");

                    ViewBag.Vendors = new SelectList(db.Vendors.ToList().Select(
                                                         x => new SelectListItem()
                    {
                        Text  = x.Name,
                        Value = x.Id.ToString()
                    }
                                                         ), "Value", "Text");

                    return(View(item));
                }

                db.VendorSupplyItems.Add(item);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(item));
        }
    public virtual TObject Update(TObject data, int id)
    {
        if (data == null)
        {
            return(null);
        }
        TObject obj = Context.Set <TObject>().Find(id);

        if (obj != null)
        {
            Context.Entry(obj).CurrentValues.SetValues(data);
            Context.SaveChanges();
        }
        return(obj);
    }
예제 #23
0
 public void Post(Event @event)
 {
     //Add logic to guard against titles being too long
     //if(@event.Title.Length > 22)
     //{
     //    throw new InvalidOperationException("Bad title length");
     //}
     using (var db = new MyDataContext())
     {
         db.Entry(@event).State = @event.EventId == 0 ?
                                  System.Data.Entity.EntityState.Added :
                                  System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
 }
예제 #24
0
        public CustomersController(MyDataContext context)
        {
            _context = context;
            if (_context.Customers.Count() == 0)
            {
                IList <Customer> customers = GetCustomers();

                foreach (var customer in customers)
                {
                    _context.Customers.Add(customer);
                }

                _context.SaveChanges();
            }
        }
        public ProductsController(MyDataContext context)
        {
            _context = context;

            if (_context.Products.Count() == 0)
            {
                IList <Product> products = new List <Product>
                {
                    new Product
                    {
                        Category    = "Goods",
                        Color       = Color.Red,
                        CreatedDate = new DateTimeOffset(2001, 4, 15, 16, 24, 8, TimeSpan.FromHours(-8)),
                        UpdatedDate = new DateTimeOffset(2011, 2, 15, 16, 24, 8, TimeSpan.FromHours(-8)),
                        Detail      = new ProductDetail {
                            Id = "3", Info = "Zhang"
                        },
                    },
                    new Product
                    {
                        Category    = "Magazine",
                        Color       = Color.Blue,
                        CreatedDate = new DateTimeOffset(2021, 12, 27, 9, 12, 8, TimeSpan.FromHours(-8)),
                        UpdatedDate = null,
                        Detail      = new ProductDetail {
                            Id = "4", Info = "Jinchan"
                        },
                    },
                    new Product
                    {
                        Category    = "Fiction",
                        Color       = Color.Green,
                        CreatedDate = new DateTimeOffset(1978, 11, 15, 9, 24, 8, TimeSpan.FromHours(-8)),
                        UpdatedDate = new DateTimeOffset(1987, 2, 25, 5, 1, 8, TimeSpan.FromHours(-8)),
                        Detail      = new ProductDetail {
                            Id = "5", Info = "Hollewye"
                        },
                    },
                };

                foreach (var product in products)
                {
                    _context.Products.Add(product);
                }

                _context.SaveChanges();
            }
        }
        public ProductsController(MyDataContext context)
        {
            _context = context;

            if (_context.Products.Count() == 0)
            {
                IList <Product> products = new List <Product>
                {
                    new Product
                    {
                        TotoId   = "1",
                        Category = "Goods",
                        Color    = Color.Red,
                        Detail   = new ProductDetail {
                            Id = "3", Info = "Zhang"
                        },
                    },
                    new Product
                    {
                        TotoId   = "2",
                        Category = "Magazine",
                        Color    = Color.Blue,
                        Detail   = new ProductDetail {
                            Id = "4", Info = "Jinchan"
                        },
                    },
                    new Product
                    {
                        TotoId   = "3",
                        Category = "Fiction",
                        Color    = Color.Green,
                        Detail   = new ProductDetail {
                            Id = "5", Info = "Hollewye"
                        },
                    },
                };

                foreach (var product in products)
                {
                    _context.Products.Add(product);
                }

                _context.SaveChanges();
            }
        }
예제 #27
0
        public void Create([FromBody] Post post, int?id)
        {
            if (ModelState.IsValid)
            {
                using (MyDataContext db = new MyDataContext())
                {
                    User userReciver = db.User.Find(id);
                    var  sessionId   = HttpContext.Current.Session.SessionID;
                    User userSender  = db.User.First(x => x.SId == sessionId);

                    post.Receiver = userReciver;
                    post.Sender   = userSender;

                    userReciver.Posts.Add(post);
                    db.SaveChanges();
                }
            }
        }
예제 #28
0
        public static int UpdateProjectByProperty <T>(this MyDataContext _db, T entity, string EntityKey) where T : class
        {
            DbSet <T> dbSet = _db.Set <T>();

            dbSet.Attach(entity);
            MemberInfo[]             members    = entity.GetType().GetMembers();
            IEnumerable <MemberInfo> properties = members.Where(m => m.MemberType == MemberTypes.Property && m.Name != EntityKey);

            foreach (MemberInfo mInfo in properties)
            {
                object o = entity.GetType().InvokeMember(mInfo.Name, BindingFlags.GetProperty, null, entity, null);
                if (o != null)
                {
                    if (o.GetType().IsPrimitive || o.GetType().IsPublic)
                    {
                        try
                        {
                            DbEntityEntry entry = _db.Entry <T>(entity);
                            entry.Property(mInfo.Name).IsModified = true;
                        }
                        catch
                        {
                        }
                    }
                }
                else
                {
                    if (mInfo.Name.ToLower() == "opening_time" || mInfo.Name.ToLower() == "checkin_time" || mInfo.Name.ToLower() == "report_effective_hours" || mInfo.Name.ToLower() == "takelook_effective_days" || mInfo.Name.ToLower() == "remarks_kind" || mInfo.Name.ToLower() == "partner_begin_time" || mInfo.Name.ToLower() == "partner_end_time" || mInfo.Name.ToLower() == "takelook_add_num" || mInfo.Name.ToLower() == "dading_add_num")
                    {
                        try
                        {
                            DbEntityEntry entry = _db.Entry <T>(entity);
                            entry.Property(mInfo.Name).IsModified = true;
                        }
                        catch
                        {
                        }
                    }
                }
            }

            return(_db.SaveChanges());
        }
        public OrdersController(MyDataContext context)
        {
            _context = context;

            if (_context.Orders.Count() == 0)
            {
                IList <Order> orders = new List <Order>
                {
                    new Order
                    {
                        Title    = "Goods",
                        Category = new Category {
                        },
                    },
                    new Order
                    {
                        Title    = "Magazine",
                        Category = new Category {
                        },
                    },
                    new Order
                    {
                        Title    = "Fiction",
                        Category = new Category {
                        },
                    },
                };

                foreach (var order in orders)
                {
                    _context.Orders.Add(order);
                }

                _context.SaveChanges();
            }
        }
예제 #30
0
 public IActionResult Store(Product product)
 {
     _context.Products.Add(product);
     _context.SaveChanges();
     return(Redirect("GetList"));
 }