public ActionResult Edit([Bind(Include = "id,teacherName,teacherEmail,teacherPicture,teacherPicName")] tblTeacher tblTeacher, HttpPostedFileBase FileUploadEdit)
        {
            if (ModelState.IsValid)
            {
                //Save image in folder

                string FileName     = Path.GetFileName(FileUploadEdit.FileName);
                string SaveLocation = Server.MapPath("~/Upload/" + FileName);
                FileUploadEdit.SaveAs(SaveLocation);

                //save image name in database

                tblTeacher.teacherPicName = "~/Upload/" + FileName;


                // byte image Save


                tblTeacher.teacherPicture = new byte[FileUploadEdit.ContentLength];
                ViewBag.TeacherPicture    = FileUploadEdit.InputStream.Read(tblTeacher.teacherPicture, 0, FileUploadEdit.ContentLength);


                db.Entry(tblTeacher).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(tblTeacher));
        }
コード例 #2
0
ファイル: HomeController.cs プロジェクト: Diana234e/Lab6
        public ActionResult EditCathedra(Cathedra cathedra, int[] selectedInstitute)
        {
            Cathedra newCathedra = db.Cathedras.Find(cathedra.ID);

            newCathedra.Name = cathedra.Name;

            newCathedra.Teacher.Clear();
            if (selectedInstitute != null)
            {
                foreach (var c in db.Teachers.Where(co => selectedInstitute.Contains(co.ID)))
                {
                    newCathedra.Teacher.Add(c);
                }
            }

            db.Entry(newCathedra).State = EntityState.Modified;
            db.SaveChanges();
            return(RedirectToAction("Cathedra"));
        }
コード例 #3
0
 public int Delete(Teacher teacher)
 {
     _teacherContext.Entry(teacher).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
     return(_teacherContext.SaveChanges());
 }
コード例 #4
0
ファイル: TransactionService.cs プロジェクト: xgtz/EF2
        public static void Test()
        {
            Database.SetInitializer <TestContext>(null);
            Database.SetInitializer <TeacherContext>(null);
            using (var context = new TestContext())
            {
                context.Database.Log = Console.WriteLine;

                var sGuid1 = Guid.NewGuid().ToString();
                var sGuid2 = Guid.NewGuid().ToString();
                var tGuid1 = Guid.NewGuid().ToString();
                var tGuid2 = Guid.NewGuid().ToString();

                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        Student s1 = new Student()
                        {
                            ID            = sGuid1,
                            Name          = "张萨姆",
                            StudentNumber = 1,
                            IsMale        = true
                        };

                        Student s2 = new Student()
                        {
                            ID            = sGuid2,
                            Name          = "张市长",
                            StudentNumber = 2,
                            IsMale        = false
                        };

                        Teacher t1 = new Teacher()
                        {
                            ID   = tGuid1,
                            Name = "张巨鹿"
                        };


                        Teacher t2 = new Teacher()
                        {
                            ID   = tGuid2,
                            Name = "老黄"
                        };

                        t1.Students.Add(s1);
                        t1.Students.Add(s2);
                        t2.Students.Add(s1);
                        t2.Students.Add(s2);
                        context.Entry(s1).State = EntityState.Added;
                        context.Entry(s2).State = EntityState.Added;
                        context.Entry(t1).State = EntityState.Added;
                        context.Entry(t2).State = EntityState.Added;

                        TeacherContext thContext = new TeacherContext(
                            context.Database.Connection,
                            contextOwnsConnection: false);
                        thContext.Database.UseTransaction(transaction.UnderlyingTransaction);

                        int c = context.SaveChanges();
                        Console.WriteLine("SaveChanges影响的行数:{0}", c);

                        //c = thContext.Database.ExecuteSqlCommand(" UPDATE STUDENT SET NAME=:NAME WHERE ID=:ID", new OracleParameter[]{
                        //    new OracleParameter("@NAME","东源"),
                        //    new OracleParameter("@ID",sGuid1),
                        //});
                        //Console.WriteLine("ExecuteSqlCommand影响的行数:{0}", c);

                        t1.Name = "落月";
                        //thContext.Entry(t1).Property(p => p.Name).IsModified = true;
                        thContext.Entry(t1).State = EntityState.Modified;
                        c = thContext.SaveChanges();
                        Console.WriteLine("thContext的SaveChanges影响的行数:{0}", c);

                        transaction.Commit();

                        try
                        {
                            Console.WriteLine("----------------------------学生信息-------------------------------------");

                            context.Entry(s1).Reload();
                            var studentLst = context.Students.Local;
                            Console.WriteLine(string.Format("学生的数量:{0}", studentLst.Count));
                            foreach (var s in studentLst)
                            {
                                Console.WriteLine(string.Format("ID:{0},Name:{1},Sex:{2}", s.ID, s.Name, s.IsMale));
                                foreach (Teacher tc in s.Teachers)
                                {
                                    Console.WriteLine(string.Format("学生对应的老师( -- ID:{0},姓名:{1} -- )", tc.ID, tc.Name));
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        transaction.Rollback();
                    }
                }
            }
        }