public ObservableCollection <IDuty> GenerateFullDutyList() { ObservableCollection <IDuty> entries = new ObservableCollection <IDuty>(); OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.Connecting, null); using (var context = new DutyContext(_dataBaseName)) { try { var dataBaseEntries = from duty in context.Duties select duty; foreach (Duty duty in dataBaseEntries) { entries.Add(duty); } } catch (Exception e) { Console.WriteLine(e); OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.ElementNotFound, e); } OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.Finished, null); return(entries); } }
public Duty ReadDutyById(Guid id) { OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.Connecting, null); using (var context = new DutyContext(_dataBaseName)) { try { Duty ret = context.Duties.First(duty => duty.Id == id); OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.Finished, null); return(ret); } catch (Exception e) { OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.ElementNotFound, e); Console.WriteLine(e); OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.Finished, null); return(null); } } }
public bool TryUpdateDuty(IDuty changedDuty) { Duty duty = null; bool successful = false; OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.Connecting, null); using (var context = new DutyContext(_dataBaseName)) { try { duty = context.Duties.First(d => d.Id == changedDuty.Id); } catch (Exception e) { Console.WriteLine(e); OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.ElementNotFound, e); } OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.Finished, null); if (duty != null) { OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.Connecting, null); try { context.Entry(duty).CurrentValues.SetValues(changedDuty); context.SaveChanges(); successful = true; } catch (Exception e) { Console.WriteLine(e); OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.UpdateFailed, e); } OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.Finished, null); } return(successful); } }
public bool TryAddDutyToDataBase(Duty duty) { bool succesful = false; OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.Connecting, null); using (var context = new DutyContext(_dataBaseName)) { try { context.Duties.Add(duty); context.SaveChanges(); succesful = true; } catch (Exception e) { Console.WriteLine(e); OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.ElementNotFound, e); } OnDatBaseConnectionStatusChanged(DataBaseConnectionStatusFlag.Finished, null); return(succesful); } }
static void Main(string[] args) { using (var db = new DutyContext("TestDataBase")) { // Create and save a new Blog Console.Write("Start"); Duty dutyA = new Duty() { Titel = "TestDuty1", Connections = new List <IDutyConnection>(), Created = DateTime.Now, Progress = 22, ProgressDescription = "TestDutyStatusDescription1", DeadLine = DateTime.MaxValue, Id = Guid.NewGuid(), LastChange = DateTime.Now, Reminders = new List <IReminder>(), Statements = new List <IStatement>() }; db.Duties.Add(dutyA); /* * TestDBClass testclass = new TestDBClass() * { * Id = 1, * Aa = "TestA" * }; * * Console.WriteLine(db.Database.Connection.Database); * Console.WriteLine(db.Database.Connection.ConnectionString); * Console.WriteLine(db.Database.Connection.DataSource); * //Console.WriteLine(db.Database.Connection.ServerVersion); * * * * db.TestDBClasses.Add(testclass); * * var found = db.TestDBClasses.Find(testclass.Id); * * db.Entry(found).Property("Aa").CurrentValue = "YOU KNOW NOTHING"; */ db.SaveChanges(); // Display all Blogs from the database var query = from duty in db.Duties orderby duty.Id select duty; Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.WriteLine(item.Titel); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }