Esempio n. 1
0
        /// <summary>
        /// Create a new HourlyEmployee object.
        /// </summary>
        /// <param name="employeeId">Initial value of the EmployeeId property.</param>
        /// <param name="name">Initial value of the Name property.</param>
        public static HourlyEmployee CreateHourlyEmployee(global::System.Int32 employeeId, global::System.String name)
        {
            HourlyEmployee hourlyEmployee = new HourlyEmployee();

            hourlyEmployee.EmployeeId = employeeId;

            hourlyEmployee.Name = name;

            return(hourlyEmployee);
        }
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var hourly = new HourlyEmployee {
                    Name = "Will Smith", Hours = 39, Rate = 7.75M
                };
                var salaried = new SalariedEmployee {
                    Name = "JoAnn Woodland", Salary = 65400M
                };
                var commissioned = new CommissionedEmployee {
                    Name = "Joel Clark", Salary = 32500M, Commission = 20M
                };
                context.Employees.AddObject(hourly);
                context.Employees.AddObject(salaried);
                context.Employees.AddObject(commissioned);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("All Employees");
                Console.WriteLine("=============");
                foreach (var emp in context.Employees)
                {
                    if (emp is HourlyEmployee)
                    {
                        Console.WriteLine("{0} Hours = {1}, Rate = {2}/hour", emp.Name, ((HourlyEmployee)emp).Hours.Value.ToString(), ((HourlyEmployee)emp).Rate.Value.ToString("C"));
                    }
                    else if (emp is CommissionedEmployee)
                    {
                        Console.WriteLine("{0} Salary = {1}, Commission = {2}%", emp.Name, ((CommissionedEmployee)emp).Salary.Value.ToString("C"), ((CommissionedEmployee)emp).Commission.ToString());
                    }
                    else if (emp is SalariedEmployee)
                    {
                        Console.WriteLine("{0} Salary = {1}", emp.Name, ((SalariedEmployee)emp).Salary.Value.ToString("C"));
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }