Click here to Skip to main content
15,891,136 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 952.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.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EFWinforms.Design
{
    /// <summary>
    /// Exposes all the EntitySets in a EntityDataSource as a list of PropertyDescriptor objects.
    /// </summary>
    /// <remarks>
    /// This is required for showing the available EntitySets in the drop down that
    /// appears when editing the "DataMember" property of a complex bound control.
    /// 
    /// Reflected from DataViewManagerListItemTypeDescriptor
    /// </remarks>
    class EntitySetTypeDescriptor : ICustomTypeDescriptor
    {
        //----------------------------------------------------------------------------
        #region ** fields

        EntityDataSource _dataSource;
        PropertyDescriptorCollection _pdc;

        #endregion

        //----------------------------------------------------------------------------
        #region ** methods

        internal EntitySetTypeDescriptor(EntityDataSource dataSource)
        {
            this._dataSource = dataSource;
        }
        internal void Reset()
        {
            this._pdc = null;
        }

        #endregion

        //----------------------------------------------------------------------------
        #region ** ICustomTypeDescriptor

        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            if (this._pdc == null)
            {
                PropertyDescriptor[] properties = null;
                int count = this._dataSource.EntitySets.Count;
                properties = new PropertyDescriptor[count];
                for (int i = 0; i < count; i++)
                {
                    properties[i] = new EntitySetPropertyDescriptor(this._dataSource.EntitySets[i]);
                }
                this._pdc = new PropertyDescriptorCollection(properties);
            }
            return this._pdc;
        }
        object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) { return this; }
        AttributeCollection ICustomTypeDescriptor.GetAttributes() { return new AttributeCollection(null); }
        string ICustomTypeDescriptor.GetClassName() { return null; }
        string ICustomTypeDescriptor.GetComponentName() { return null; }
        TypeConverter ICustomTypeDescriptor.GetConverter() { return null; }
        EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() { return null; }
        PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() { return null; }
        object ICustomTypeDescriptor.GetEditor(Type editorBaseType) { return null; }
        EventDescriptorCollection ICustomTypeDescriptor.GetEvents() { return new EventDescriptorCollection(null); }
        EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) { return new EventDescriptorCollection(null); }
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() { return ((ICustomTypeDescriptor)this).GetProperties(null); }

        #endregion
    }

    /// <summary>
    /// Custom PropertyDescriptor used by the EntitySetTypeDescriptor (above)
    /// to expose EntitySets as properties.
    /// </summary>
    /// <remarks>
    /// Reflected from DataTablePropertyDescriptor
    /// </remarks>
    class EntitySetPropertyDescriptor : PropertyDescriptor
    {
        //----------------------------------------------------------------------------
        #region ** fields

        EntitySet _view;

        #endregion

        //----------------------------------------------------------------------------
        #region ** ctor

        internal EntitySetPropertyDescriptor(EntitySet view)
            : base(view.Name, null)
        {
            _view = view;
        }

        #endregion

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

        public override bool CanResetValue(object component)
        {
            return false;
        }
        public override bool Equals(object other)
        {
            if (other is EntitySetPropertyDescriptor)
            {
                EntitySetPropertyDescriptor descriptor = (EntitySetPropertyDescriptor)other;
                return (descriptor._view == this._view);
            }
            return false;
        }
        public override int GetHashCode()
        {
            return this._view.GetHashCode();
        }
        public override object GetValue(object component)
        {
            return _view.List;
        }
        public override void ResetValue(object component) { }
        public override void SetValue(object component, object value) { }
        public override bool ShouldSerializeValue(object component) { return false; }
        public override Type ComponentType { get { return typeof(EntitySet); } }
        public override bool IsReadOnly { get { return false; } }
        public override Type PropertyType { get { return typeof(IBindingList); } }

        #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