public ActionResult Menu() { List<CMS_CategoryModels> models = null; using (TransactionScope scope = new TransactionScope())// 事务对象 { using (var ctx = new StarDBContext()) { EFAction<CMS_CategoryModels> model = new EFAction<CMS_CategoryModels>(); model.ctx = ctx; models = model.GetAll().ToList(); } } return View(GetMenu(models)); }
public void TestEF() { // Add Person To DB using (TransactionScope scope = new TransactionScope())// 事务对象 { using (var ctx = new StarDBContext()) { //var person = new Person(); //person.FirstName = "Robbert2"; //person.MiddleName = "Allen2"; //person.LastName = "Doe2"; //content.Person.Add(person); //var table1 = new Table1(); //table1.Field1 = "tt2"; //table1.Field2 = "ddd"; //table1.Field3 = "sss"; //ctx.Table2.Add(table1); //ctx.SaveChanges(); // exe sql command //var parameters = new DbParameter[]{ // new SqlParameter(parameterName:"Field1",value:"tt"), // new SqlParameter(parameterName:"Field2",value:"ddd") //}; //var result = new Table1(); var efAction = new EFAction<Table1>(); efAction.ctx = ctx; var table1 = efAction.Get(1); var efAction2 = new EFAction<Person>(); efAction2.ctx = ctx; var person = efAction2.Get(1); //var res = ctx.Database.SqlQuery<Table1>("select * from Table1 where Field1=@Field1 and Field2=@Field2", parameters).ToList(); //ctx.Person.Find(1); var a = 1; } } }