static void Main(string[] args) { //Add record using (var db = new MyDBContext()) { db.Templetes.Add(new Templete { Name = "Test111" }); db.Templetes.Add(new Templete { Name = "Test11" }); db.SaveChanges(); } //delete record from id using (var db = new MyDBContext()) { var del = db.Templetes.SingleOrDefault(x => x.Id == 1); if (del != null) { db.Templetes.Remove(del); db.SaveChanges(); } } //edit record using (var db = new MyDBContext()) { var edit = db.Templetes.SingleOrDefault(x => x.Id == 10); edit.Name = "UpdateName"; db.SaveChanges(); } //find using (var db = new MyDBContext()) { var find = db.Templetes.Where(x => x.Name == "UpdateName").FirstOrDefault(); Console.WriteLine("Find lines Id: " + find.Id); } //Writing using (var db = new MyDBContext()) { foreach (var templete in db.Templetes) { Console.WriteLine("Template {0} = {1}", templete.Id, templete.Name); } } Console.ReadKey(); }
/// <summary> /// Add a new instrument or update an existing instrument in the database. /// </summary> /// <param name="instrument"></param> /// <param name="updateIfExists"></param> /// <param name="saveChanges">Set to true if saving to db should be done.</param> /// <returns>True if the insertion or update succeeded. False if it did not.</returns> public Instrument AddInstrument(Instrument instrument, bool updateIfExists = false, bool saveChanges = true) { if (instrument.IsContinuousFuture) { throw new Exception("Cannot add continuous futures using this method."); } using (var context = new MyDBContext()) { //make sure data source is set and exists if (instrument.Datasource == null || !context.Datasources.Any(x => x.Name == instrument.Datasource.Name)) { throw new Exception("Failed to add instrument: invalid datasource."); } //make sure exchange exists, if it is set if (instrument.Exchange != null && !context.Exchanges.Any(x => x.Name == instrument.Exchange.Name)) { throw new Exception("Failed to add instrument: exchange does not exist."); } if (instrument.PrimaryExchange != null && !context.Exchanges.Any(x => x.Name == instrument.PrimaryExchange.Name)) { throw new Exception("Failed to add instrument: primary exchange does not exist."); } //check if the instrument already exists in the database or not var existingInstrument = context.Instruments.SingleOrDefault(x => (x.ID == instrument.ID) || (x.Symbol == instrument.Symbol && x.DatasourceID == instrument.DatasourceID && x.ExchangeID == instrument.ExchangeID && x.Expiration == instrument.Expiration)); if (existingInstrument == null) //object doesn't exist, so we add it { //attach the datasource, exchanges, etc. so it doesn't try to add them //also load sessions at the same time context.Datasources.Attach(instrument.Datasource); if (instrument.PrimaryExchange != null) { context.Exchanges.Attach(instrument.PrimaryExchange); context.Entry(instrument.PrimaryExchange).Collection(x => x.Sessions).Load(); } if (instrument.PrimaryExchangeID != instrument.ExchangeID && instrument.Exchange != null) { context.Exchanges.Attach(instrument.Exchange); context.Entry(instrument.Exchange).Collection(x => x.Sessions).Load(); } //if necessary, load sessions from teplate or exchange if (instrument.SessionsSource == SessionsSource.Exchange && instrument.Exchange != null) { instrument.Sessions = instrument.Exchange.Sessions.Select(x => x.ToInstrumentSession()).ToList(); } else if (instrument.SessionsSource == SessionsSource.Exchange && instrument.Exchange == null) { instrument.SessionsSource = SessionsSource.Custom; instrument.Sessions = new List <InstrumentSession>(); } else if (instrument.SessionsSource == SessionsSource.Template) { instrument.Sessions = new List <InstrumentSession>(); var template = context.SessionTemplates.Include("Sessions").FirstOrDefault(x => x.ID == instrument.SessionTemplateID); if (template != null) { foreach (TemplateSession s in template.Sessions) { instrument.Sessions.Add(s.ToInstrumentSession()); } } } context.Instruments.Add(instrument); context.Database.Connection.Open(); if (saveChanges) { context.SaveChanges(); } Log(LogLevel.Info, string.Format("Instrument Manager: successfully added instrument {0}", instrument)); return(instrument); } else if (updateIfExists) //object exist, but we want to update it { Log(LogLevel.Info, string.Format("Instrument Manager: updating existing instrument ID {0} with the following details: {1}", existingInstrument.ID, instrument)); context.Entry(existingInstrument).CurrentValues.SetValues(instrument); if (saveChanges) { context.SaveChanges(); } return(existingInstrument); } } return(null); //object exists and we don't update it }
public DiscountResponsitory(MyDBContext context) : base(context) { discountEntity = context.Set <Discount>(); }
public ValuesController(MyDBContext context) { this._context = context; }
public UnitOfWork(MyDBContext context) { _context = context; }
public LoaiController(MyDBContext context) { _context = context; }
public VencimientoController(MyDBContext context) { _context = context; }
public PublisherController(MyDBContext myDBContext) { _ctx = myDBContext; }
public LeaveController(MyDBContext _context) { this.context = _context; }
public FileStoragesController(MyDBContext context, IMapper mapper) { _context = context; _mapper = mapper; }
public ChuDeDao() { db = new MyDBContext(); }
public PromotionRepository(MyDBContext context) { _context = context; }
public DepartmentManager() { DB = new MyDBContext(); }
public MuonSachController() { context = new MyDBContext(); }
public HistoryController(MyDBContext context) { _context = context; }
public DisciplineResponsitory(MyDBContext context) : base(context) { disciplineEntity = context.Set <Discipline>(); }
public Func_ThongTinCaNhan() { context = new MyDBContext(); }
public DocGiaController() { context = new MyDBContext(); }
public AdminController() { _context = new MyDBContext(); }
public TblOutletsController(MyDBContext context) { _context = context; }
public ActionResult SaveOrUpdate(UserRelationship model) { //daca e ceva null mesaj de eroare if (model.ClientId == 0 || model.CourierId == 0 || model.ClientPackageId == 0) { TempData["msg"] = "<script>alert('Toate campurile sunt obligatorii!');</script>"; return(View(model)); } else { if (ModelState.IsValid) { using (MyDBContext dc = new MyDBContext()) { //vf daca exista duplicat in baza, valid sau invalid var duplicate = dc.UserRelationship .Where(a => a.ClientId == model.ClientId && a.CourierId == model.CourierId && a.ClientPackageId == model.ClientPackageId) .FirstOrDefault(); if (model.Id > 0) //Update { //daca editarea nu are duplicat in baza if (duplicate == null) { //editam var original = dc.UserRelationship.Where(a => a.Id == model.Id).FirstOrDefault(); if (original != null) { model.Valid = true; dc.Entry(original).CurrentValues.SetValues(model); } } else { if (duplicate.Valid == false) { model.Valid = true; dc.Entry(duplicate).CurrentValues.SetValues(model); } else { TempData["msgDuplicate"] = "<script>alert('Aceasta inregistrare exista deja in baza de date si este valida');</script>"; return(View(model)); } } } else //Save { if (duplicate == null) { model.Valid = true; dc.UserRelationship.Add(model); } else { //daca exista un duplicat dar e invalid, il validam si nu cream o noua inreg if (duplicate.Valid == false) { model.Valid = true; dc.Entry(duplicate).CurrentValues.SetValues(model); } else { TempData["msgDuplicate"] = "<script>alert('Aceasta inregistrare exista deja in baza de date si este valida');</script>"; return(View(model)); } } } dc.SaveChanges(); } } return(View("Index")); } }
public FlightDetailsRepository(MyDBContext context) : base(context) { _context = context; }
public CustomersController() { _context = new MyDBContext(); }
public ProductRepository(MyDBContext myDBContext) { _myDBContext = myDBContext; }
public YeuCauThemSachController() { context = new MyDBContext(); }
public MoviesController() { _context = new MyDBContext(); }
public CanvasUsersController(MyDBContext context) { _context = context; }
public List <Instrument> FindInstruments(Expression <Func <Instrument, bool> > pred, MyDBContext context = null) { var query = GetIQueryable(ref context); var instruments = query.Where(pred).ToList(); foreach (Instrument i in instruments) { //hack because we can't load these in the normal way, see comment below if (i.Exchange != null) { i.Exchange.Sessions.ToList(); } } return(instruments); }
public HangHoasController(MyDBContext context) { _context = context; }
public OderDetailsController(MyDBContext context) { _context = context; }
public ExperienceRepository(MyDBContext context) : base(context) { }