Click here to Skip to main content
15,886,673 members
Articles / Desktop Programming / Windows Forms

Entity Framework in WinForms

Rate me:
Please Sign up or sign in to vote.
4.89/5 (142 votes)
28 Jul 2014CPOL29 min read 947.2K   58.1K   425  
A component that makes it easy to use Entity Framework in WinForms projects, including design-time binding support.
using System;
using System.Globalization;
using System.Reflection;
using System.Data;
using System.Data.Objects;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace EFWinforms.Design
{
    /// <summary>
    /// TypeConverter that shows a list of types that inherit from ObjectContext and
    /// can be assigned to the ObjectContextType property.
    /// </summary>
    public class ObjectContextTypeTypeConverter : ReferenceConverter
    {
        //----------------------------------------------------------------------------
        #region ** ctor

        public ObjectContextTypeTypeConverter()
            : base(typeof(Type))
        {
        }

        #endregion

        //----------------------------------------------------------------------------
        #region ** overrides

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            List<Type> values = new List<Type>();

            // value for no data source
            values.Add(null);
            
            // all types that derive from ObjectContext
            foreach (Type t in GetObjectContextTypes(context))
            {
                values.Add(t);
            }

            // done
            return new StandardValuesCollection(values);
        }
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                var values = GetObjectContextTypes(context);
                foreach (var t in values)
                {
                    if (t.ToString() == (string)value)
                    {
                        return t;
                    }
                }
                return null;
            }
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string) && value is Type)
            {
                return value.ToString();
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        List<Type> GetObjectContextTypes(ITypeDescriptorContext context)
        {
            var values = new List<Type>();
            var tds = context.GetService(typeof(ITypeDiscoveryService)) as ITypeDiscoveryService;
            if (tds != null)
            {
                foreach (Type t in tds.GetTypes(typeof(ObjectContext), true))
                {
                    if (t.IsPublic && t.IsVisible && !t.IsAbstract)
                    {
                        values.Add(t);
                    }
                }
            }
            return values;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Brazil Brazil
Software Architect/Developer with several years experience creating and delivering software.

Full-stack Web development (including React, Firebase, TypeScript, HTML, CSS), Entity Framework, C#, MS SQL Server.

Passionate about new technologies and always keen to learn new things as well as improve on existing skills.

Comments and Discussions