Dynamically compare 2 entity property values in VB.net

Had a scenario today where we had to compare the property values of view model against a domain entity, so I wrote simple windows client sample to dynamically compare properties with the same name to dynamically compare values and if the values are different just add the names of these properties to a list

On Load I create 2 entities _person and _student; person has name and age properties; _student has name, age, sex and school. On comparing _person and _student we find “name” property values differ, age is same, sex and school does not apply to person class. So the only property I want in difference list is “name”

Code Snippet
  1. Public Class Form1
  2.     Private _person As Person
  3.     Private _student As Student
  4.     Private Sub Compare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Compare.Click
  5.         Dim diffList As New List(Of String)
  6.         For Each personProp In _person.GetType.GetProperties
  7.             Dim studentValue = _student.GetType.GetProperty(personProp.Name).GetValue(_student, Nothing)
  8.             Dim personValue = _person.GetType.GetProperty(personProp.Name).GetValue(_person, Nothing)
  9.             If studentValue <> personValue Then
  10.                 diffList.Add(personProp.Name)
  11.             End If
  12.         Next
  13.     End Sub
  14.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  15.         _person = New Person With {.name = “A”, .age = 10}
  16.         _student = New Student With {.name = “B”, .age = 10, .sex = “m”, .school = “XYZ”}
  17.     End Sub
  18. End Class

Code first – Enums Supported in EF June 2011 CTP

Support for enumeration has been one of the long desired requests in Entity Framework. As soon as I saw the June 2011 CTP, I had to test it. Here is what I did

1. I created windows project TestEfJuneCtp


2. I changed the Target framework to “Microsoft Entity Framework June 2011 CTP”


3.    Created a couple of classes project, order and an enumeration ProjectStatusType



4.    Project has a property projectStatus of type ProjectStatusType, this would have been ignored in EF 4.1. meaning when you generate the database projectStatus column would not appear in the project table. Now in the current CTP it does appear of type Int.

5.    Then I set up my Context, I did add reference to system.data.entity v4.2


6.    Using Enums to set values


7.    If you want to set value other than the one in Enum then just Directcast
8.    Since I set projectStatus to InProgress, it is set to 0 in the database column
9.    Get Values from Enum properties


When you do projectEntity.projectStatus it returns “0”, but when you do do projectEntity.projectStatus.ToString it returns “InProgress”

10.    Also the power of enumeration can be more profound when writing code sinec intellisence can pick up the type