コード例 #1
0
        public static bool SaveEmployee(EmployeeEntity employee)
        {
            bool lSuccess = false;


            using (var context = new MyDataContext("DefaultConnectionString"))
            {
                context.Database.CreateIfNotExists();

                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        var query = context.Employees.Where(p => (p.Id == employee.Id));
                        foreach (var rec in query)
                        {
                            // Delete record
                            context.Employees.Remove(rec);
                        }
                        // Insert record
                        context.Employees.Add(employee);
                        context.SaveChanges();
                        transaction.Commit();
                        lSuccess = true;
                    }
                    catch (Exception exc)
                    {
                        transaction.Rollback();
                        throw exc;
                    }
                }
            }
            return(lSuccess);
        }
コード例 #2
0
        static EmployeeProvider()
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString))
            {
                // Create database if not exists
                using (var contextDB = new MyDataContext(connection, false))
                {
                    contextDB.Database.CreateIfNotExists();
                }
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();

                try
                {
                    using (var context = new MyDataContext(connection, false))
                    {
                        // Passing an existing transaction to the context
                        context.Database.UseTransaction(transaction);

                        if (context.Employees.Count() == 0)
                        {
                            context.Employees.Add(
                                new EmployeeEntity()
                            {
                                Id          = 1,
                                Name        = "Mark",
                                Gender      = "Male",
                                DateOfBirth = DateTime.Parse("10/10/1980")
                            });
                            context.Employees.Add(
                                new EmployeeEntity()
                            {
                                Id          = 2,
                                Name        = "Mary",
                                Gender      = "Female",
                                DateOfBirth = DateTime.Parse("11/10/1981")
                            });
                            context.Employees.Add(
                                new EmployeeEntity()
                            {
                                Id          = 3,
                                Name        = "John",
                                Gender      = "Male",
                                DateOfBirth = DateTime.Parse("8/10/1979")
                            });

                            context.SaveChanges();
                        }
                    }
                    transaction.Commit();
                }
                catch (Exception exc)
                {
                    transaction.Rollback();
                    throw exc;
                }
            }
        }
コード例 #3
0
        public static bool SaveEmployee(EmployeeEntity employee)
        {
            bool lSuccess = false;
            bool lExist   = false;

            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString))
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();

                try
                {
                    using (var context = new MyDataContext(connection, false))
                    {
                        // Passing an existing transaction to the context
                        context.Database.UseTransaction(transaction);

                        var query = context.Employees.Where(p => (p.Id == employee.Id));
                        foreach (var rec in query)
                        {
                            // Update all property
                            PropertyInfo[] properties = typeof(EmployeeEntity).GetProperties();
                            foreach (PropertyInfo property in properties)
                            {
                                property.SetValue(rec, property.GetValue(employee));
                            }
                            lExist = true;
                        }

                        if (!lExist)
                        {
                            // Insert record
                            context.Employees.Add(employee);
                        }

                        context.SaveChanges();
                    }
                    transaction.Commit();
                    lSuccess = true;
                }
                catch (Exception exc)
                {
                    transaction.Rollback();
                    // throw exc;
                }
            }
            return(lSuccess);
        }
コード例 #4
0
        public static bool SaveEmployee(EmployeeEntity employee)
        {
            bool lSuccess = false;

            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString))
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();

                try
                {
                    using (var context = new MyDataContext(connection, false))
                    {
                        // Passing an existing transaction to the context
                        context.Database.UseTransaction(transaction);

                        var query = context.Employees.Where(p => (p.Id == employee.Id));
                        foreach (var rec in query)
                        {
                            // Delete record
                            context.Employees.Remove(rec);
                        }
                        // Insert record
                        context.Employees.Add(employee);
                        context.SaveChanges();
                    }
                    transaction.Commit();
                    lSuccess = true;
                }
                catch (Exception exc)
                {
                    transaction.Rollback();
                    // throw exc;
                }
            }
            return(lSuccess);
        }
コード例 #5
0
        static EmployeeProvider()
        {
            using (var context = new MyDataContext("DefaultConnectionString"))
            {
                if (!context.Database.Exists())
                {
                    context.Database.CreateIfNotExists();
                }
            }

            Database.SetInitializer(new MigrateDatabaseToLatestVersion <MyDataContext, Migrations.Configuration>(true));

            using (var context = new MyDataContext("DefaultConnectionString"))
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        if (context.Employees.Count() == 0)
                        {
                            context.Employees.Add(
                                new FullTimeEmployeeEntity()
                            {
                                Id           = 1,
                                Name         = "Mark",
                                Gender       = "Male",
                                DateOfBirth  = DateTime.Parse("10/10/1980"),
                                AnnualSalary = 50000
                            });
                            context.Employees.Add(
                                new PartTimeEmployeeEntity()
                            {
                                Id          = 2,
                                Name        = "Mary",
                                Gender      = "Female",
                                DateOfBirth = DateTime.Parse("11/10/1981"),
                                HoursWorked = 20,
                                HourlyPay   = 10
                            });
                            context.Employees.Add(
                                new FullTimeEmployeeEntity()
                            {
                                Id           = 3,
                                Name         = "John",
                                Gender       = "Male",
                                DateOfBirth  = DateTime.Parse("8/10/1979"),
                                AnnualSalary = 20000
                            });

                            context.SaveChanges();
                        }

                        transaction.Commit();
                    }
                    catch (Exception exc)
                    {
                        transaction.Rollback();
                        throw exc;
                    }
                }
            }
        }