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();

            #region EMPLOYEE SELF-RELATIONSHIPS
            //var query = db.Employees
            //    .Include(e => e.Address)
            //    .Include(e => e.Supervisor)
            //    .Include(e => e.Buddies)
            //    .ToList();
            #endregion

            #region ONE-TO-ONE
            //var query = db.Employees
            //    .Include(e => e.Address)
            //    .ToList();
            //var lefts = db.Lefts
            //    .Include(l => l.Right)
            //    .ToList();
            //var rights = db.Rights
            //    .Include(r => r.Left)
            //    .ToList();
            #endregion

            #region ONE-TO-MANY
            var query = db.Departments
                .Include(d => d.Employees)
                .ToList();

            var depToDelete = db.Departments.Find(1);
            db.Departments.Remove(depToDelete);

            db.SaveChanges();

            var newQuery = db.Employees
                .ToList();
            #endregion

            #region MANY-TO-MANY JOIN TABLE
            //var query = db.Employees
            //    .Include(e => e.Projects)
            //    .ToList();
            #endregion

            #region END
            string script = ((IObjectContextAdapter)db).ObjectContext.CreateDatabaseScript();
            Console.ReadLine();
            #endregion
        }
Esempio n. 2
0
        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();
        }
Esempio n. 3
0
        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();
        }