1 class Program2 {3 Static voidMain (string[] args)4 {5 //DbContext context = Contextfactory.getcontext ();6 //OrderInfo oi = new OrderInfo ()7 //{8 //ordertitle= "Samsung mobile"9 //};Ten //Oi. CustomerInfo = new CustomerInfo () One //{ A //customername= "Chun Yuchon", - //Customerdate=datetime.now - //}; the //context. Set<orderinfo> (). Add (OI); - //context. SaveChanges (); - //Console.WriteLine ("OK"); - //Console.readkey (); + - //Test, + addcustomerinfo (); A addorderinfo (); at Save (); -Console.WriteLine ("OK"); - Console.readkey (); - - } - /// <summary> in /// - /// </summary> to Private Static voidSave () + { -DbContext context =Contextfactory.getcontext (); the context. SaveChanges (); * } $ Panax Notoginseng Private Static voidAddcustomerinfo () - { theDbContext context =Contextfactory.getcontext (); +Context. Set<customerinfo> (). ADD (NewCustomerInfo () A { theCustomerName ="Big Zhang Wei", +Customerdate =DateTime.Now - }); $ $ } - - Private Static voidAddorderinfo () the { -DbContext context =Contextfactory.getcontext ();WuyiContext. Set<orderinfo> (). ADD (NewOrderInfo () the { -ordertitle="Seven Stars Blades", WuCustomerid=7 - }); About } $ } - /// <summary> - ///gets the class of the context object - /// </summary> A Public Static classcontextfactory + { the Public StaticDbContext getcontext () - { $ //Gets the context object from the data slot, guaranteeing the object's single theDbContext context = CallContext.GetData ("DbContext") asDbContext; the if(Context = =NULL) the { the //when the data slot does not exist. Create a context object and put it in a data slot -Context =Neweftestentities (); inCallcontext.setdata ("DbContext", context); the } the returncontext; About } the}
----Efcontext objects Get---Singleton mode from the data slot