Exemple #1
0
        public string Insert(Customer customer)
        {
            using (var dbContext = new NorthwindEntities())
            {
                dbContext.Customers.Add(customer);
                dbContext.SaveChanges();

                return customer.CustomerID;
            }
        }
Exemple #2
0
 public void Delete(string id)
 {
     using (var dbContext = new NorthwindEntities())
     {
         var customer = dbContext.Customers.FirstOrDefault(c => c.CustomerID == id);
         if (customer != null)
         {
             dbContext.Customers.Remove(customer);
             dbContext.SaveChanges();
         }
     }
 }
Exemple #3
0
 public void Update(string id, string newCompanyName)
 {
     using (var dbContext = new NorthwindEntities())
     {
         var customer = dbContext.Customers.FirstOrDefault(c => c.CustomerID == id);
         if (customer != null)
         {
             customer.CompanyName = newCompanyName;
             dbContext.SaveChanges();
         }
     }
 }
Exemple #4
0
 public void Delete(string id)
 {
     using (var dbContext = new NorthwindEntities())
     {
         var customer = dbContext.Customers.FirstOrDefault(c => c.CustomerID == id);
         if (customer != null)
         {
             dbContext.Customers.Remove(customer);
             dbContext.SaveChanges();
         }
     }
 }
Exemple #5
0
 public void Update(string id, string newCompanyName)
 {
     using (var dbContext = new NorthwindEntities())
     {
         var customer = dbContext.Customers.FirstOrDefault(c => c.CustomerID == id);
         if (customer != null)
         {
             customer.CompanyName = newCompanyName;
             dbContext.SaveChanges();
         }
     }
 }
Exemple #6
0
        public string Insert(string customerID, string companyName)
        {
            using(var dbContext = new NorthwindEntities())
            {
                var customer = new Customer()
                {
                    CustomerID = customerID,
                    CompanyName = companyName,
                };

                dbContext.Customers.Add(customer);
                dbContext.SaveChanges();

                return customer.CustomerID;
            }
        }
Exemple #7
0
        public string Insert(string customerID, string companyName)
        {
            using (var dbContext = new NorthwindEntities())
            {
                var customer = new Customer()
                {
                    CustomerID  = customerID,
                    CompanyName = companyName,
                };

                dbContext.Customers.Add(customer);
                dbContext.SaveChanges();

                return(customer.CustomerID);
            }
        }
Exemple #8
0
        private static void OpenTwoDbContexts()
        {
            var randomNumber = new Random().Next(10, 99);
            var id           = "ABC" + randomNumber;

            using (var firstDbContext = new NorthwindEntities())
            {
                var newCustomer = new Customer()
                {
                    CustomerID = id, CompanyName = "COMPANY" + id
                };
                firstDbContext.Customers.Add(newCustomer);
                firstDbContext.SaveChanges();
                var customerSelectedByFirstDbContext = firstDbContext.Customers.Where(c => c.CustomerID == id).FirstOrDefault();
                customerSelectedByFirstDbContext.CompanyName = "Updated By First";

                using (var secondDbContext = new NorthwindEntities())
                {
                    var customerSelectedBySecondDbContext = secondDbContext.Customers.Where(c => c.CustomerID == id).FirstOrDefault();
                    customerSelectedBySecondDbContext.CompanyName = "Updated By Second";
                    secondDbContext.SaveChanges();
                    var selectedBySecondDb = secondDbContext.Customers.Where(c => c.CustomerID == id).FirstOrDefault();
                    Console.WriteLine("SecondDbContext => CompanyName: {0}", selectedBySecondDb.CompanyName);
                }

                try
                {
                    firstDbContext.SaveChanges();
                    var selectedByFirstDb = firstDbContext.Customers.Where(c => c.CustomerID == id).FirstOrDefault();
                    Console.WriteLine("FirstDbContext => CompanyName: {0}", selectedByFirstDb.CompanyName);
                }
                catch (DbUpdateConcurrencyException)
                {
                    Console.WriteLine("OptimisticConcurrencyException handled on FirstDbContext.SaveChanges()");
                }
            }

            using (var dbContext = new NorthwindEntities())
            {
                var customer = dbContext.Customers.Where(c => c.CustomerID == id).FirstOrDefault();
                Console.WriteLine("Actual CompanyName: {0}", customer.CompanyName);
            }
        }
Exemple #9
0
        private static void CloneNorthwindByConnectionStringFromAppConfig(string cloneName)
        {
            using (var dbContext = new NorthwindEntities("name=" + cloneName))
            {
                dbContext.Database.CreateIfNotExists();
                var randomNumber = new Random().Next(10, 99);
                dbContext.Customers.Add(new Customer()
                {
                    CustomerID = "ABC" + randomNumber, CompanyName = "CLONE COMPANY"
                });
                dbContext.SaveChanges();

                var customers = dbContext.Customers.ToList();
                Console.WriteLine("Database Name: " + cloneName);
                foreach (var customer in customers)
                {
                    Console.WriteLine(customer.CustomerID + " " + customer.CompanyName);
                }
            }
        }
Exemple #10
0
        private static void CloneNorthwind(string cloneName)
        {
            string connectionString = @"metadata=res://*/NorthwindEntities.csdl|res://*/NorthwindEntities.ssdl|res://*/NorthwindEntities.msl;provider=System.Data.SqlClient;provider connection string=';data source=.\SQLEXPRESS;initial catalog=" + cloneName + ";integrated security=True;MultipleActiveResultSets=True;App=EntityFramework';";

            using (var dbContext = new NorthwindEntities(connectionString))
            {
                dbContext.Database.CreateIfNotExists();
                var randomNumber = new Random().Next(10, 99);
                dbContext.Customers.Add(new Customer()
                {
                    CustomerID = "ABC" + randomNumber, CompanyName = "CLONE COMPANY"
                });
                dbContext.SaveChanges();

                var customers = dbContext.Customers.ToList();
                Console.WriteLine("Database Name: " + cloneName);
                foreach (var customer in customers)
                {
                    Console.WriteLine(customer.CustomerID + " " + customer.CompanyName);
                }
            }
        }