Reputation: 477
I have a problem about managing concurrency with Entity Framework.
I defined a Person class (I'll show only the interesting parts)
public class Person
{
public Guid Id { get; set; }
public string Name { get; set; }
public Guid? CategoryId { get; set; }
public virtual Category Category { get; set; }
public byte[] TimeStamp { get; set; }
}
and a class for configuration
public class PersonConfiguration : EntityTypeConfiguration<Person>
{
public PersonConfiguration() : base()
{
HasKey(p => p.Id);
Property(p => p.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(p => p.TimeStamp)
.IsConcurrencyToken()
.HasColumnType("timestamp")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
}
}
Similarly I defined the Category class and the respective class for configuration
public class Category
{
public Guid Id { get; set; }
public string Title { get; set; }
public byte[] TimeStamp { get; set; }
}
public class CategoryConfiguration : EntityTypeConfiguration<Category>
{
public CategoryConfiguration() : base()
{
HasKey(c => c.Id);
Property(c => c.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(c => c.TimeStamp)
.IsConcurrencyToken()
.HasColumnType("timestamp")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
}
}
In the CommitChanges method I catch the DbUpdateConcurrencyException exception and with a 'database wins' strategy I reload the entity from DB.
public int CommitChanges()
{
ret = -1;
try
{
ret = _context.SaveChanges();
}
catch (DbUpdateConcurrencyException e)
{
(e as DbUpdateConcurrencyException).Entries.Single().Reload();
throw new UowUpdateConcurrencyException();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return ret;
}
For the purposes of this question we assume that only one entity at a time is saved in DB
There's no problem if I save a Category. All the tests are ok.
The problem raises when i try to save a Person (previously loaded trough the same context) and there's a concurrency violation. I debug that line
(e as DbUpdateConcurrencyException).Entries.Single().Reload();
Surprise. The exception is thrown correctly but the entity that come up from the DB is of type Category instead of Person.
This return Category.
(e as DbUpdateConcurrencyException).Entries.ToList()[0].Entity.GetType()
If I disable the concurrency check for the Category by removing the Timestamp property in the Category class everything works fine
I think there's some problem with the navigation property that i defined in the Person class.
What do you think?
Thanks!
UPDATE
It seems that the problem is in the binding that I set between a ComboBox and the Category property.
If I comment it everything works fine
<ComboBox ItemsSource="{Binding Path=CategoryList}"
DisplayMemberPath="Title"
SelectedValuePath="Id"
SelectedItem="{Binding Path=Category, Mode=TwoWay}" />
If I bind the combo to the CategoryId instead to the Category the problem still remains.
<ComboBox ItemsSource="{Binding Path=CategoryList}"
DisplayMemberPath="Title"
SelectedValuePath="Id"
SelectedValue="{Binding Path=CategoryId, Mode=TwoWay}" />
Upvotes: 2
Views: 1730
Reputation: 477
I found a solution that seems more like a workaround so I don't accept this as an answer for one or two days in hope that someone can give an explanation.
The solution consists in load the CategoryList that is ItemsSource for the combo through a different context than that used for Person and for the save operation. This prevent to include the Category in the scope of the save.
Finally, we must set the binding to the CategoryId property and not the Category property.
Upvotes: 1