我已经尝试过几天了解这个问题并且有大量关于工作单元和TransactionScope的教程,但是我找不到任何关于这两者的讨论。任何帮助非常感谢!
我正在使用具有工作单元模式的Entity Framework和每种类型的存储库。根据下面的简单代码,我有一个Member和MembershipDefinition实体。我想创建一个链接两者的Membership实体,但是当我创建Membership对象时我想根据一些业务逻辑查询DB的最大值。因此,我需要使用某种DB事务来阻止另一个线程在我的线程将Membership对象写回DB之前增加db中的值。
如果我使用存储过程,这将非常简单,但我无法弄清楚如何使用纯c#...
下面的代码在数据库中创建了100个Membership实体,其中包含重复的MembershipNumbers。我需要使用这种方法来确保在c#代码中生成的所有成员编号都是唯一的。
class Program
{
static void Main(string[] args)
{
var p = new Program();
p.Go();;
}
public void Go()
{
long memberId;
long membershipDefId;
using(var unitOfWork = new UnitOfWork())
{
// Setup - create test club and member entities
var testUsername = ("TestUserName" + Guid.NewGuid()).Substring(0, 29);
var member = new Member()
{
UserName = testUsername
};
var testmemebrshpDefName = ("TestMembershipDef" + Guid.NewGuid()).Substring(0, 29);
var membershipDefinition = new ClubMembershipDefinition()
{
ClubId = 1,
Name = testmemebrshpDefName
};
unitOfWork.MemberRepository.Add(member);
unitOfWork.MembershipDefinitionRepository.Add(membershipDefinition);
unitOfWork.Save();
memberId = member.Id;
membershipDefId = membershipDefinition.Id;
}
Task[] tasks = new Task[100];
// Now try to add a membership to the Member object, linking it to the test Club's single Club Definition
for (int i = 0; i < 100; i++)
{
var task = new Task(() => CreateMembership(memberId, membershipDefId));
tasks[i] = task;
task.Start();
}
Task.WaitAll(tasks);
}
private void CreateMembership(long memberId, long membershipDefId)
{
using (var unitOfWork = new UnitOfWork())
{
var member = unitOfWork.MemberRepository.GetById(memberId);
var membershipDef = unitOfWork.MembershipDefinitionRepository.GetById(membershipDefId);
var membership = new ClubMembership()
{
ClubMembershipDefinition = membershipDef
};
membership.MembershipNumber = (unitOfWork.MembershipRepository.GetMaxMembershipNumberForClub(membershipDef.ClubId) ?? 0) + 1;
member.ClubMemberships.Add(membership);
unitOfWork.Save();
}
}
}
public class UnitOfWork : IUnitOfWork, IDisposable
{
internal ClubSpotEntities _dbContext = new ClubSpotEntities();
internal MemberRepository _memberRepository;
internal MembershipRepository _membershipRepository;
internal MembershipDefinitionRepository _membershiDefinitionpRepository;
public MemberRepository MemberRepository
{
get
{
if (_memberRepository == null)
_memberRepository = new MemberRepository(_dbContext);
return _memberRepository; ;
}
}
public MembershipRepository MembershipRepository
{
get
{
if (_membershipRepository == null)
_membershipRepository = new MembershipRepository(_dbContext);
return _membershipRepository; ;
}
}
public MembershipDefinitionRepository MembershipDefinitionRepository
{
get
{
if (_membershiDefinitionpRepository == null)
_membershiDefinitionpRepository = new MembershipDefinitionRepository(_dbContext);
return _membershiDefinitionpRepository; ;
}
}
public virtual int Save()
{
return _dbContext.SaveChanges();
}
private bool _disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
if (disposing)
{
_dbContext.Dispose();
}
}
this._disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
public class MembershipRepository
{
ClubSpotEntities _dbContext = new ClubSpotEntities();
public MembershipRepository(){}
public MembershipRepository(ClubSpotEntities dbContext)
{
_dbContext = dbContext;
}
public IEnumerable<ClubMembership> GetAll()
{
return _dbContext.Set<ClubMembership>().ToList<ClubMembership>();
}
public ClubMembership GetById(long id)
{
return _dbContext.ClubMemberships.First(x => x.Id == id);
}
public long? GetMaxMembershipNumberForClub(long clubId)
{
return _dbContext.ClubMemberships.Where(x => x.ClubMembershipDefinition.ClubId == clubId).Max(x => x.MembershipNumber);
}
public ClubMembership Add(ClubMembership entity)
{
return _dbContext.Set<ClubMembership>().Add(entity);
}
public void Delete(ClubMembership membership)
{
_dbContext.Set<ClubMembership>().Remove(membership);
}
public void Save()
{
_dbContext.SaveChanges();
}
}
public partial class ClubMembership
{
public long Id { get; set; }
public long MembershipDefId { get; set; }
public Nullable<long> MemberId { get; set; }
public Nullable<long> MembershipNumber { get; set; }
public virtual ClubMembershipDefinition ClubMembershipDefinition { get; set; }
public virtual Member Member { get; set; }
}
public partial class ClubMembershipDefinition
{
public ClubMembershipDefinition()
{
this.ClubMemberships = new HashSet<ClubMembership>();
}
public long Id { get; set; }
public long ClubId { get; set; }
public string Name { get; set; }
public virtual ICollection<ClubMembership> ClubMemberships { get; set; }
}
public partial class Member
{
public Member()
{
this.ClubMemberships = new HashSet<ClubMembership>();
}
public long Id { get; set; }
public string UserName { get; set; }
public virtual ICollection<ClubMembership> ClubMemberships { get; set; }
}
答案 0 :(得分:2)
您可以在实例化新的UnitOfWork时创建事务范围,并在完成时提交它。这不是完整的例子:
class UnitOfWork
{
ClubSpotEntities _dbContext;
TransactionScope _transaction;
public UnitOfWork()
{
_dbContext = new ClubSpotEntities();
_transaction = new TransactionScope();
}
public void Complete()
{
_dbContext.SaveChanges();
_transaction.Complete();
}
...
}
<强> UPD:强> 正如史蒂文所说,这不是你问题的解决方案。而且UnitOfWork无法帮助你,在这种情况下,TransactionScope也不是解决方案。 EF不支持您想要使用的悲观锁,但您可以尝试使用此solution。