Пример #1
0
        static void Main()
        {
            // 7.Try to open two different data contexts and perform concurrent changes on the same records.
            // What will happen at SaveChanges() ?
            // How to deal with it ?

            using (var northwind1 = new NorthwindEntities())
            {
                using (var northwind2 = new NorthwindEntities())
                {
                    Console.WriteLine(new string('-', 35));
                    Console.WriteLine("First context");
                    Console.WriteLine(new string('-', 35));
                    var person1 = northwind1.Employees.FirstOrDefault();
                    Console.WriteLine("Initial:{0}", person1.FirstName);
                    person1.FirstName = "XAXAXA";
                    Console.WriteLine("Changed:{0}", person1.FirstName);

                    // Change by Entity state Unchanged Modified Detached
                    var dbEntry = northwind1.Entry(person1);
                    dbEntry.State = EntityState.Unchanged;
                    northwind1.SaveChanges();

                    Console.WriteLine(new string('-', 35));
                    Console.WriteLine("Second context");
                    Console.WriteLine(new string('-', 35));
                    var person2 = northwind2.Employees.FirstOrDefault();
                    Console.WriteLine("Initial:{0}", person2.FirstName);
                    person2.FirstName = "UHAHAH";
                    Console.WriteLine("Changed:{0}", person2.FirstName);

                    northwind2.SaveChanges();
                }
            }
        }
Пример #2
0
        static void Main()
        {
            // 6.Create a database called NorthwindTwin with the same structure as Northwind using the features from DbContext.
            // Find for the API for schema generation in MSDN or in Google.

            // Get the application configuration file.
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            // Create a connection string element and save it to the configuration file.

            // Create a connection string element.
            ConnectionStringSettings csSettings =
                    new ConnectionStringSettings("NorthwindEntities",
                   @"metadata=res://*/NorthwindModel.csdl|res://*/NorthwindModel.ssdl|res://*/NorthwindModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=NorthwindTwin;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"", "System.Data.EntityClient");

            // Get the connection strings section.
            ConnectionStringsSection csSection = config.ConnectionStrings;

            // Add the new element.
            //csSection.ConnectionStrings.Add(csSettings);

            // Save the configuration file.
            config.Save(ConfigurationSaveMode.Modified);

            using (var northwindEntities = new NorthwindEntities())
            {
                Console.WriteLine(northwindEntities.Database.CreateIfNotExists());
            }
        }
Пример #3
0
 private static void DeleteCustomer(string customerId)
 {
     using (var northwindEntities = new NorthwindEntities())
     {
         Customer customer = GetCustomerById(northwindEntities, customerId);
         northwindEntities.Customers.Remove(customer);
         northwindEntities.SaveChanges();
     }
 }
Пример #4
0
 private static string CreateNewCustomer(string customerId, string companyName, string contactName)
 {
     using (var northwindEntities = new NorthwindEntities())
     {
         Customer newCustomer = new Customer
         {
             CustomerID = customerId,
             CompanyName = companyName,
             ContactName = contactName
         };
         northwindEntities.Customers.Add(newCustomer);
         northwindEntities.SaveChanges();
         return newCustomer.CustomerID;
     }
 }
Пример #5
0
        public static void Main()
        {
            // 8.By inheriting the Employee entity class create a class which allows employees to access their corresponding territories as property of type EntitySet<T>.

            // class EmployeeExtend in EntityFrameworkModels

            using (var northwind = new NorthwindEntities())
            {
                var employee = northwind.Employees.Find(2);

                foreach (var item in employee.TerritoryProperty)
                {
                    Console.WriteLine("Employee:{0} Teritory desctiption: {1}", employee.FirstName, item.TerritoryDescription);
                }
            }
        }
Пример #6
0
        private static void FindAllCustomersWithOrdersFrom1997UsingSQL()
        {
            using (var northwindEntities = new NorthwindEntities())
            {
                string nativeSqlQuery =
                "SELECT * FROM dbo.Orders o " +
                "JOIN dbo.Customers c " +
                "ON o.CustomerID = c.CustomerID " +
                "WHERE (OrderDate BETWEEN '1997.01.01' AND '1997.12.31') AND ShipCountry = 'Canada' " +
                "ORDER BY c.CompanyName";

                var customers = northwindEntities.Database.SqlQuery<OrdersToCanada>(nativeSqlQuery);

                foreach (var item in customers)
                {
                    Console.WriteLine(string.Format("Company: {0}, CompanyContact: {1}", item.CompanyName, item.ContactName));
                }
            }
        }
Пример #7
0
        private static void FindAllSalesBySpecifiedRegionAndPeriod(string region, DateTime startDate, DateTime endDate)
        {
            using (var northwindEntities = new NorthwindEntities())
            {
                var sales = northwindEntities
                .Order_Details
                .Where(o => o.Order.ShipRegion == region)
                .Where(o => o.Order.OrderDate >= startDate)
                .Where(o => o.Order.OrderDate <= endDate)
                .Select(c => new
                {
                    Customer = c.Order.Customer.CompanyName,
                    Date = c.Order.OrderDate,
                    Product = c.Product.ProductName
                })
                .ToList();

                foreach (var item in sales)
                {
                    Console.WriteLine("Customer:{0} , OrderDate:{1}, Product:{2}", item.Customer, item.Date, item.Product);
                }
            }
        }
Пример #8
0
        private static void FindAllCustomersWithOrdersFrom1997()
        {
            using (var northwindEntities = new NorthwindEntities())
            {
                var customers = northwindEntities
                    .Orders
                    .Where(o => o.ShipCountry == "Canada")
                    .Where(o => o.OrderDate >= new DateTime(1997, 01, 01))
                    .Where(o => o.OrderDate <= new DateTime(1997, 12, 31))
                    .OrderBy(o => o.Customer.CompanyName)
                    .Select(e => new
                    {
                        Company = e.Customer.CompanyName,
                        ContactName = e.Customer.ContactName,
                        Country = e.ShipCountry
                    })
                    .ToList();

                foreach (var item in customers)
                {
                    Console.WriteLine(item);
                }
            }
        }
Пример #9
0
 private static void ModifyCustomerName(string customerId, string companyName, string contactName)
 {
     using (var northwindEntities = new NorthwindEntities())
     {
         Customer customer = GetCustomerById(northwindEntities, customerId);
         customer.CompanyName = companyName;
         customer.ContactName = contactName;
         northwindEntities.SaveChanges();
     }
 }
Пример #10
0
 private static Customer GetCustomerById(NorthwindEntities northwindEntities, string customerId)
 {
     Customer customer = northwindEntities
         .Customers
         .FirstOrDefault(
         p => p.CustomerID == customerId);
     return customer;
 }