For concurrency control i Write "VersionCheck" function in my Context class,I need to dynamically load Context object and check if version is the same as the current context object RowVersion. now i use switch statement. (code below) and also, Is there more convenient way for version control?
p.s. RowVersion is TimeStamp type in Database.
public class SchoolContext : DbContext
{
public DbSet<Group> Groups { get; set; }
public DbSet<Person> Persons { get; set; }
public bool VersionCheck(string objName)
{
var dbc = new SchoolContext();
byte[] bt1 = null;
byte[] bt2 = null;
switch (objName)
{
case "Person":
dbc.Persons.Load();
bt1 = dbc.Persons.SingleOrDefault(p => p.Id == 1).RowVersion;
bt2 = this.Persons.Local.SingleOrDefault(p => p.Id == 1).RowVersion;
break;
case "Group":
dbc.Groups.Load();
bt1 = dbc.Groups.SingleOrDefault(p => p.Id == 1).RowVersion;
bt2 = this.Groups.Local.SingleOrDefault(p => p.Id == 1).RowVersion;
break;
}
if (bt1 == null && bt2 == null)
{
throw new Exception("One of the Variable is null!");
return true;
}
for (int i = 0; i < bt1.Length; i++)
{
if (bt1[i] != bt2[i])
{
MessageBox.Show("Current object changed!");
return false;
}
}
return true;
}
}