我正在编写一个非常简单的多用户WPF应用程序,每个用户都可以修改/删除自己的数据。意味着每个用户都可以查看其他用户的所有数据,但不允许对其进行修改或删除。 整个应用程序运行良好,直到同时有2个或更多客户端点击savechanges方法,然后有时会遇到数据库死锁。
事务(进程ID 57)与另一个进程在锁定资源上死锁,因此被选择为死锁受害者。重新运行事务。
我不知道为什么会出现这种死锁,因为不同用户不可能同时修改或删除相同数据。
我写了一个简短的程序来演示这种行为。最后的delete方法模拟两个用户同时删除自己的数据。
这是我的DbContext和数据模型:
public class Context : DbContext
{
public DbSet<City> dbsCities { get; set; }
public DbSet<House> dbsHouses { get; set; }
public DbSet<Person> dbsPersons { get; set; }
public Context() : base(@"Server=.\SQLExpress;Database=test;Trusted_Connection=Yes;Connection Timeout=5")
{
Configuration.AutoDetectChangesEnabled = true;
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<City>().HasMany(c => c.Houses).WithRequired(c => c.City).WillCascadeOnDelete(true);
modelBuilder.Entity<House>().HasMany(p => p.Residents).WithRequired(p => p.House).WillCascadeOnDelete(true);
}
}
public class City
{
public Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<House> Houses { get; set; }
public City()
{
Id = Guid.NewGuid();
Houses = new List<House>();
}
}
public class House
{
public Guid Id { get; set; }
public int Number { get; set; }
public string Code { get; set; }
public virtual City City { get; set; }
public virtual ICollection<Person> Residents { get; set; }
public House()
{
Id = Guid.NewGuid();
Residents = new List<Person>();
}
}
public class Person
{
public Guid Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public virtual House House { get; set; }
public Person()
{
Id = Guid.NewGuid();
}
}
这里是生成测试数据的代码:
using (Context ctx = new Context())
{
List<City> cities = new List<City>() { new City() { Name = "New York" } };
for (int h = 1; h <= 50; h++)
{
string code = "A";
if (h % 2 == 0)
code = "B";
House house = new House() {Number = h, Code = code };
cities[0].Houses.Add(house);
for (int i = 0; i <= 100; i++)
house.Residents.Add(new Person() { Firstname = "A", Lastname = "B" });
}
ctx.dbsCities.AddRange(cities);
ctx.SaveChanges();
}
最后导致死锁的方法
private void Delete(object sender, RoutedEventArgs e)
{
string[] to = new string[] {"A", "B"};
Parallel.ForEach(to, code =>
{
DeleteHouses(code);
});
}
private void DeleteHouses(string code)
{
using (var ctx = new Context())
{
ctx.Database.Log += Console.WriteLine;
var todel = ctx.dbsHouses.Where(d=>d.Code == code);
if (todel != null)
{
ctx.dbsHouses.RemoveRange(todel);
ctx.SaveChanges();
}
}
}
我不知道为什么会陷入僵局,我在做什么错吗?