多个Savechanges工作单元EF核心

时间:2020-02-07 20:27:48

标签: asp.net-core transactions entity-framework-core ef-core-2.2

我试图从here了解工作单元和存储库模式。 它具有如下工作单元类

public interface IUnitOfWork : IDisposable
    {
        IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;

        int Commit();
    }

    public interface IUnitOfWork<TContext> : IUnitOfWork where TContext : DbContext
    {
        TContext Context { get; }
    }

并通过类似的服务进行调用

public class SomeService
{
    private readonly IUnitOfWork _uow;

    public SomeService(IUnitOrWork unit )
    {
        _uow = unit;
    }

    public void SomeMethod(SomeClass entity)
    {
        _uow.GetRepository<SomeClass>().Add(entity);
       _uow.Commit();

    }
}

我对文章有疑问,如果有人可以详细说明。

  • 如果我需要多次调用savechanges怎么办,因为我需要将第一个savechanges中的ID用于第二个savechanges中。我希望这两个保存更改要么成功保存要么全部回滚

0 个答案:

没有答案