Esempio n. 1
0
        /// <summary>
        /// Provides a search for company name
        /// </summary>
        /// <param name="value">text to use for filter</param>
        /// <param name="options">Type of search</param>
        /// <remarks>
        /// By default EF is case sensitive. Here we use ToLower to allow
        /// case insensitive search but a fragile method is modifying the underlying table
        /// https://milinaudara.wordpress.com/2015/02/04/case-sensitive-search-using-entity-framework-with-custom-annotation/
        /// </remarks>
        public void CompanyNameFilter(string value, FilterOptions options)
        {
            using (NorthWindEntity context = new NorthWindEntity())
            {
                switch (options)
                {
                case FilterOptions.StartsWith:
                    DataSource = context.Customers.Filter(cust => cust.CompanyName.ToLower().StartsWith(value.ToLower())).ToList();
                    break;

                case FilterOptions.Contains:
                    DataSource = context.Customers.Filter(cust => cust.CompanyName.ToLower().Contains(value.ToLower())).ToList();
                    break;

                case FilterOptions.EndsWith:
                    DataSource = context.Customers.Filter(cust => cust.CompanyName.ToLower().EndsWith(value.ToLower())).ToList();
                    break;

                case FilterOptions.Equals:
                    DataSource = context.Customers.Filter(cust => cust.CompanyName.Equals(value, StringComparison.OrdinalIgnoreCase)).ToList();
                    break;

                default:
                    break;
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Provides a method to update an existing customer and check for validation errors.
 /// If there are issues in validation the entry is reverted back to it's former state.
 /// 
 /// Also, the validation errors are reported back to the user interface.
 /// 
 /// </summary>
 /// <param name="EditedCustomer"></param>
 /// <returns></returns>
 public bool UpdateCustomer(Customer EditedCustomer)
 {
     using (NorthWindEntity context = new NorthWindEntity())
     {
         try
         {
             context.Entry(EditedCustomer).State = EntityState.Modified;
             context.SaveChanges();
             ValidationMessage = "";
             return true;
         }
         catch (System.Data.Entity.Validation.DbEntityValidationException ef)
         {                   
             UndoAll(context);
             ValidationMessage = ef.DbEntityValidationExceptionToString();
             return false;
         }
         catch (Exception ex)
         {
             UndoAll(context);
             ValidationMessage = "Update failed";
             return false;
         }
     }
 }
Esempio n. 3
0
 public void GetCustomers()
 {
     using (NorthWindEntity context = new NorthWindEntity())
     {
         DataSource = context.Customers.Select(cust => cust).ToList();
     }
 }
Esempio n. 4
0
 /// <summary>
 /// Return a distinct list of contact titles
 /// </summary>
 /// <remarks>
 /// In a normalized database the titles would be in a reference table.
 /// </remarks>
 public void GetContactTitles()
 {
     using (NorthWindEntity context = new NorthWindEntity())
     {
         ContactList = context
             .Customers
             .DistinctBy(cust => cust.ContactTitle)
             .OrderBy(cust => cust.ContactTitle)
             .Select(cust => cust.ContactTitle)
             .ToList();
     }
 }
Esempio n. 5
0
 /// <summary>
 /// Shows how to get column names for Customer table
 /// </summary>
 private void GetColumNames()
 {
     ColumnNames = new List <string>();
     using (NorthWindEntity context = new NorthWindEntity())
     {
         var objectContext   = ((IObjectContextAdapter)context).ObjectContext;
         var storageMetadata = ((EntityConnection)objectContext.Connection).GetMetadataWorkspace().GetItems(DataSpace.SSpace);
         var entityProps     = (from sm in storageMetadata where sm.BuiltInTypeKind == BuiltInTypeKind.EntityType select sm as EntityType);
         // For your project, open the model browser to get the name for the model, will have namespace.Store
         var metaData = (from m in entityProps where m.FullName == "NORTHWNDModel.Store.Customers" select m.DeclaredProperties).ToList();
         foreach (var topItem in metaData)
         {
             foreach (var item in topItem)
             {
                 ColumnNames.Add(item.Name);
             }
         }
     }
 }