Binding RadGridView to Entity Framework Using Code First Approach

Problem

Entity Framework`s Database First approach provides very little control over the models used in the application. It automatically generates a lot of files which should not be altered and at times versioning of the database becomes very difficult.

Solution

The Code First approach, on the other hand, delivers exactly the opposite. That is why, especially in recent times and for new projects, the developer community chooses this way to interact with SQL Server. Another great advantage is that the database can be entirely managed by the application. Changes in the models are also automatically reflected by the ORM engine at the end migrated to the database itself.

We are going to take this example project even further by implementing Repository Pattern and Unit of Work. Our implementation consists of three projects each defining a separate layer of the application.

RadGridViewEFCodeFirst.Models – responsible for the business objects

RadGridViewEFCodeFirst.Data – data layer for connecting to SQL Server

RadGridViewEFCodeFirst.WinFormsClient – client demo application

Our RadGridView control will be setup in hierarchy and the screenshot below shows the result at the end:

In our Models project, let`s first define several business objects:

[C#]

publicclassOrderType

{

privateICollection<Order> orders;

privateICollection<Shipper> shippers;

publicOrderType()

{

this.orders = newHashSet<Order>();

this.shippers = newHashSet<Shipper>();

}

publicintOrderTypeId { get; set; }

publicvirtualICollection<Order> Orders

{

get

{

returnthis.orders;

}

set

{

this.orders = value;

}

}

publicvirtualICollection<Shipper> Shippers

{

get

{

returnthis.shippers;

}

set

{

this.shippers = value;

}

}

publicstringDescription { get; set; }

}

[VB]

PublicClassOrderType

Privatem_orders AsICollection(Of Order)

Privatem_shippers AsICollection(Of Shipper)

PublicSubNew()

Me.m_orders = NewHashSet(Of Order)()

Me.m_shippers = NewHashSet(Of Shipper)()

EndSub

PublicPropertyOrderTypeId() AsInteger

Get

Returnm_OrderTypeId

EndGet

Set(value AsInteger)

m_OrderTypeId = Value

EndSet

EndProperty

Privatem_OrderTypeId AsInteger

PublicOverridablePropertyOrders() AsICollection(Of Order)

Get

ReturnMe.m_orders

EndGet

Set(value AsICollection(Of Order))

Me.m_orders = value

EndSet

EndProperty

PublicOverridablePropertyShippers() AsICollection(Of Shipper)

Get

ReturnMe.m_shippers

EndGet

Set(value AsICollection(Of Shipper))

Me.m_shippers = value

EndSet

EndProperty

PublicPropertyDescription() AsString

Get

Returnm_Description

EndGet

Set(value AsString)

m_Description = Value

EndSet

EndProperty

Privatem_Description AsString

EndClass

[C#]

publicclassOrder

{

publicintOrderId { get; set; }

publicintOrderTypeId { get; set; }

publicvirtualOrderType OrderType { get; set; }

publicstringDescription { get; set; }

publicboolIsFinished { get; set; }

}

[VB]

PublicClassOrder

PublicPropertyOrderId() AsInteger

Get

Returnm_OrderId

EndGet

Set(value AsInteger)

m_OrderId = Value

EndSet

EndProperty

Privatem_OrderId AsInteger

PublicPropertyOrderTypeId() AsInteger

Get

Returnm_OrderTypeId

EndGet

Set(value AsInteger)

m_OrderTypeId = Value

EndSet

EndProperty

Privatem_OrderTypeId AsInteger

PublicOverridablePropertyOrderType() AsOrderType

Get

Returnm_OrderType

EndGet

Set(value AsOrderType)

m_OrderType = Value

EndSet

EndProperty

Privatem_OrderType AsOrderType

PublicPropertyDescription() AsString

Get

Returnm_Description

EndGet

Set(value AsString)​

m_Description = Value

EndSet

EndProperty

Privatem_Description AsString

PublicPropertyIsFinished() AsBoolean

Get

Returnm_IsFinished

EndGet

Set(value AsBoolean)

m_IsFinished = Value

EndSet

EndProperty

Privatem_IsFinished AsBoolean

EndClass

[C#]

publicclassShipper

{

publicintShipperId { get; set; }

publicintOrderTypeId { get; set; }

publicvirtualOrderType OrderType { get; set; }

publicstringName { get; set; }

publicstringAddress { get; set; }

}

[VB]

PublicClassShipper

PublicPropertyShipperId() AsInteger

Get

Returnm_ShipperId

EndGet

Set(value AsInteger)

m_ShipperId = Value

EndSet

EndProperty

Privatem_ShipperId AsInteger

PublicPropertyOrderTypeId() AsInteger

Get

Returnm_OrderTypeId

EndGet

Set(value AsInteger)

m_OrderTypeId = Value

EndSet

EndProperty

Privatem_OrderTypeId AsInteger

PublicOverridablePropertyOrderType() AsOrderType

Get

Returnm_OrderType

EndGet

Set(value AsOrderType)

m_OrderType = Value

EndSet

EndProperty

Privatem_OrderType AsOrderType

PublicPropertyName() AsString

Get

Returnm_Name

EndGet

Set(value AsString)

m_Name = Value

EndSet

EndProperty

Privatem_Name AsString

PublicPropertyAddress() AsString

Get

Returnm_Address

EndGet

Set(value AsString)

m_Address = Value

EndSet

EndProperty

Privatem_Address AsString

EndClass

Now, in our Data project we have to define the interfaces needed by the Repository Pattern and define the common functionality:

Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks for appropriate markings.