static void Main(string[] args) { // SetupObjects(); IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration(); config.Common.Add(new TransparentActivationSupport()); using (IObjectContainer db = Db4oEmbedded.OpenFile(GetDbFileName())) { // QBE Employee target = new Employee { Name = "Felipe" }; IObjectSet qberesult = db.QueryByExample(target); Employee emp = (Employee)qberesult.Next(); Console.WriteLine(emp.Name + ", " + emp.Address.PropertyName); foreach (Project proj in emp.Projects) { Console.WriteLine(proj.ProjectName); } // NQ IList<Project> nqresult = db.Query<Project>(delegate(Project pr) { return pr.ProjectName.StartsWith("Se"); }); Project projnq = nqresult.First<Project>(); Console.WriteLine(projnq.ProjectName); // wait for key press before ending Console.ReadLine(); db.Dispose(); } }
static void Main(string[] args) { // set database initializer Database.SetInitializer<CompanyContext>(new CompanyContextInitializer()); CompanyContext db = new CompanyContext(); db.Configuration.LazyLoadingEnabled = false; db.Configuration.ProxyCreationEnabled = true; db.Configuration.AutoDetectChangesEnabled = false; // new entity var newEmp = new Employee { Name = "Kimi" }; var newEmpEntity = db.Entry(newEmp); Console.WriteLine("NEW ENTITY NOT ATTACHED"); Console.WriteLine(string.Format("Employee {0}, state = {1}", newEmp.Name, newEmpEntity.State)); PrintTrackedEmployees(db); // attach new entity db.Entry(newEmp).State = System.Data.EntityState.Added; Console.WriteLine("\nNEW ENTITY ATTACHED"); PrintTrackedEmployees(db); #region GET DATA // get some objects, eager load so we can access all necessary properties with LL disabled var jenson = db.Employees .Include(e => e.Projects) .Include(e => e.Department) .Where(e => e.EmployeeId==1) .First(); var fernando = db.Employees .Include(e => e.Projects) .Include(e => e.Department) .Where(e => e.EmployeeId == 3) .First(); var marketing = db.Departments .Include(d => d.Employees) .Where(d => d.DepartmentId == 1) .First(); var sales = db.Departments .Include(d => d.Employees) .Where(d => d.DepartmentId == 2) .First(); var adCampaign = db.Projects .Include(p => p.Employees) .Where(p => p.ProjectId == 1) .First(); var website = db.Projects .Include(p => p.Employees) .Where(p => p.ProjectId == 2) .First(); #endregion Console.WriteLine("\nRETRIEVED FROM DATABASE"); PrintDetails(jenson); PrintTrackedEmployees(db); // change Department for Employee jenson.Department = sales; // was marketing // jenson in sales, sales has 4 employees including jenson db.ChangeTracker.DetectChanges(); // will fix-up always Console.WriteLine("\nJENSON CHANGED DEPARTMENT TO SALES"); PrintDetails(jenson); // remove Employee from Project and add Project to Employee jenson.Projects.Remove(adCampaign); // was on adcampaign and website fernando.Projects.Add(website); // was not on any projects // jenson on website only, website includes fernando db.Employees.Add(new Employee { Name = "new" }); // will fix-up if AutoDetectChangesEnabled //db.ChangeTracker.DetectChanges(); Console.WriteLine("\nJENSON REMOVED ADCAMPAIGN, FERNANDO ADDED WEBSITE"); PrintDetails(jenson); marketing.Employees.Add(jenson); // was in sales // jenson in marketing, marketing has 2 employees including jenson db.SaveChanges(); // will fix-up if AutoDetectChangesEnabled //db.ChangeTracker.DetectChanges(); Console.WriteLine("\nMARKETING ADDED JENSON"); PrintDetails(jenson); Console.WriteLine("Press any key to finish..."); Console.ReadLine(); }
private static void PrintDetails(Employee emp) { Console.WriteLine(string.Format("Employee: {0} is in Department: {1}\n", emp.Name, emp.Department.Name)); Console.WriteLine(string.Format("Department: {0} has Employees:", emp.Department.Name)); foreach (var e in emp.Department.Employees) { Console.WriteLine(string.Format("{0}", e.Name)); } Console.WriteLine(string.Format("\nEmployee: {0} is on Projects:", emp.Name)); foreach (var p in emp.Projects) { Console.WriteLine(string.Format("{0}", p.Name)); Console.WriteLine(string.Format(" Project: {0} has Employees:", p.Name)); foreach (var e in p.Employees) { Console.WriteLine(string.Format(" {0}", e.Name)); } } }
static void Main(string[] args) { // set database initializer Database.SetInitializer<CompanyContext>(new CompanyContextInitializer()); // initialise EF Profiler HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize(); CompanyContext db = new CompanyContext(); // Create Address objects Address ad1 = new Address { AddressId = 3, PropertyName = "LINQ Tower", PropertyNumber = 99, PostCode = "G1 1XX" }; Address ad2 = new Address { AddressId = 4, PropertyName = "EF House", PropertyNumber = 101, PostCode = "G4 0XX" }; // Create Employee objects and associate with Addresses Employee emp1 = new Employee { Name = "Fernando", Username = "******", PhoneNumber = "1234", Address = ad1 }; SalariedEmployee emp2 = new SalariedEmployee { Name = "Felipe", Username = "******", PhoneNumber = "5678", Address = ad2, DepartmentName = "Sales", // set FK property PayGrade = 3 }; // Create Department object Department dep1 = new Department { DepartmentName = "Sales" }; // Add Employee objects to Department dep1.Employees.Add(emp1); //dep1.Employees.Add(emp2); db.Departments.Add(dep1); //db.Employees.Add(emp1); db.Employees.Add(emp2); db.SaveChanges(); // dispose context and open a new context db.Dispose(); db = new CompanyContext(); // reload entity from database //db.Entry(dep1).Reload(); db.Configuration.LazyLoadingEnabled = false; db.Configuration.ProxyCreationEnabled = false; List<Employee> employees = db.Employees .Include(e => e.Address) .Include(e => e.Department) .ToList<Employee>(); Address address = employees[0].Address; var query = from d in db.Departments where d.DepartmentName == "Marketing" select d; //string command = query.ToString(); //Console.WriteLine(command + "\n"); Department department = query.FirstOrDefault(); // set break point here and inspect objects with debugger Console.WriteLine("Press any key to finish..."); Console.ReadLine(); }
public void AddEmployee(Employee emp) { Employees.Add(emp); emp.Projects.Add(this); }
// METHODS public void AddEmployee(Employee newEmployee) { employees.Add(newEmployee); }
static void Main(string[] args) { // note trace listener in app.config configured to log SQL to file trace.log var db = Database.OpenNamedConnection("CompanyConnection"); #region DYANMIC OBJECTS // insert var newDynamic = db.Employees.Insert(name: "Jenson", username: "******", phonenumber: "4735", addressID: 2); // SimpleRecord var emp1 = db.Employees.FindByName("Fernando"); Console.WriteLine("SIMPLERECORD: Id: {0}, Name: {1}, Phone: {2}", emp1.employeeID, emp1.name, emp1.phonenumber); // update emp1.phonenumber = "1111"; db.Employees.Update(emp1); // SimpleQuery and Select (with join) // this is a natural join which works because FK is defined in db // can do explicit join if join field is not FK var emps1 = db.Employees.FindAll(db.Employees.AddressID == 2) .select( db.Employees.name, db.Employees.phonenumber, // note column in db is nchar(10) so filed has fixed length db.Employees.Addresses.propertyname ); Console.WriteLine("SIMPLEQUERY, JOIN"); foreach (var em in emps1) { Console.WriteLine("Name: {0}, Phone: {1}, Address.propertyname: {2}", em.name, em.phonenumber, em.propertyname); } // SimpleQuery and With to get related object // note table name is Addresses even though an employee will only have one address var emps2 = db.Employees.FindAll(db.Employees.AddressID == 2) .WithAddresses(); Console.WriteLine("SIMPLEQUERY, WITH"); foreach (var em in emps2) { Console.WriteLine("Name: {0}, Phone: {1}, Addresses.propertyname: {2}", em.name, em.phonenumber, em.Addresses.propertyname); } // delete db.Employees.Delete(name: "Jenson"); #endregion #region POCOs // insert POCO var newEmp = new Employee{Name ="Checo", Username ="******", PhoneNumber = "1846", AddressID = 2 }; db.Employees.Insert(newEmp); // implicit cast to POCO Employee emp2 = db.Employees.FindByName("Checo"); Console.WriteLine("SINGLE POCO: Id: {0}, Name: {1}", emp2.EmployeeID, emp2.Name); // update POCO emp2.Name = "Sergio"; db.Employees.Update(emp2); // implicit cast to POCO collection List<Employee> emps3 = db.Employees. FindAll(db.Employees.AddressID == 2); Console.WriteLine("POCO COLLECTION"); foreach (var em in emps3) { Console.WriteLine("Id: {0}, Name: {1}", em.EmployeeID, em.Name); } // explicit cast to POCO collection with eager loading // variable em in loop is POCO Employee type with associated Address object // note naviagation property in Employee has to be called Addresses to match related // table name even though it is a reference to a single Address object var emps4 = db.Employees.FindAll(db.Employees.AddressID == 2) .WithAddresses() .Cast<Employee>(); Console.WriteLine("POCO COLLECTION EAGER LOAD"); foreach (var em in emps4) { Console.WriteLine("Id: {0}, Name: {1}, Addresses.propertyname: {2}", em.EmployeeID, em.Name, em.Addresses.PropertyName); } #endregion Console.ReadLine(); }
static void Main(string[] args) { // SetupObjects(); // Open embedded server and open client for that server using (IObjectServer server = Db4oClientServer.OpenServer(GetDbFileName(), 0)) { using (IObjectContainer client = server.OpenClient()) { // set activation depth (default = 5) client.Ext().Configure().ActivationDepth(1); // QBE Employee target = new Employee { Name = "Felipe" }; IObjectSet qberesult = client.QueryByExample(target); Employee emp = (Employee)qberesult.Next(); Console.WriteLine(emp.Name + ", " + emp.Address.PropertyName); // activate a property dynamically Address empAddress = emp.Address; client.Activate(empAddress, 1); // NQ (type-safe) IList<Project> nqresult = client.Query<Project>(delegate(Project pr) { return pr.ProjectName.StartsWith("Se"); }); Project projnq = nqresult.First<Project>(); Console.WriteLine(projnq.ProjectName); // SODA IQuery sodaquery = client.Query(); sodaquery.Constrain(typeof(Project)); sodaquery.Descend("projectName") // name of field, not property .Constrain("Se") .StartsWith(true); // case sensitive Project projsoda = (Project)sodaquery.Execute().Next(); Console.WriteLine(projsoda.ProjectName); // LINQ (type-safe) var projectQuery = from Project p in client where p.ProjectName.StartsWith("Se") select p; Project proj = projectQuery.First<Project>(); Console.WriteLine(proj.ProjectName); var projectQuery2 = from Project p in client where p.ProjectName.StartsWith("Se") select new { name = p.ProjectName, headcount = p.Employees.Count }; var proj2 = projectQuery2.First(); Console.WriteLine(proj2.name + ", " + proj2.headcount); // wait for key press before ending Console.ReadLine(); server.Dispose(); } } }