如何解决在工作单元中实施数据库事务以进行多个保存调用
我对工作单元和存储库模式有些陌生。我已经实现了一个,但是现在在保存需要数据库生成值的数据时遇到了一些问题。
以下是模型:
public class Account
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long AccountCode { get; set; }
[Required]
[StringLength(100)]
public string Title { get; set; }
[StringLength(500)]
public string Description { get; set; }
public long? OpeningLedgerId { get; set; }
public Ledger Ledger { get; set; }
}
public class Ledger
{
[Key]
public long LedgerId { get; set; }
public DateTime TransactionDateTime { get; set; }
public long AccountCode { get; set; }
public double Debit { get; set; }
public double Credit { get; set; }
[Required]
public string Narration { get; set; }
[ForeignKey(nameof(AccountCode))]
public Account Account { get; set; }
}
现在通过以下方法详细了解创建方法
public void Create(AccountDTO accountDTO)
{
Account account = new Account
{
AccountCode = accountDTO.AccountCode,Title = accountDTO.Title,Description = accountDTO.Description
};
account.Ledger = new Ledger
{
TransactionDateTime = DateTime.Now,AccountCode = accountDTO.AccountCode,Narration = "Opening Transaction",Debit = accountDTO.Debit,Credit = accountDTO.Credit,};
_unitOfWork.AccountRepository.Insert(account);
_unitOfWork.LedgerRepository.Insert(account.Ledger);
account.OpeningLedgerId = account.Ledger.LedgerId;
_unitOfWork.Save();
}
此行account.OpeningLedgerId = account.Ledger.LedgerId;
要求在将分类帐保存到数据库时生成该值。
那么,在UnitOfWork中处理此问题或处理数据库级别事务的最佳方法是什么?我想到的一件事是在UnitOfWork类中创建两个类似BeginTransaction(); CommitTransaction();
的方法,并在此类情况下显式调用它们。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。