2

使用新功能 SavePoints 将 EF 核心版本 3.1 升级到 5(使用事务手册时自动创建)

我在 SQL Server 2016 中有一个名为“内容”的内存优化表。

当我调用“SaveChanges”命令时,系统会抛出异常“内存优化表不支持创建保存点”。如何关闭保存点?

注意:如果我使用 TransactionScope 则通过成功。

请给我处理这种情况的解决方案 https://docs.microsoft.com/en-us/ef/core/saving/transactions

4

1 回答 1

0

我想在没有外键的两个表中插入行。

在 startup.cs 文件中

public void ConfigureServices(IServiceCollection services)
    {
        services.AddHealthChecks();
        services.AddDbContext<wUtilityContentContext>(opts => opts.UseSqlServer(
           Algorithm.Decrypt(Configuration["ConnectionStrings:wUtilityContent_ConnectionString"]),
           b => b.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name)));

        services.AddUnitOfWorkPool(optionsBuilder =>
        {
            optionsBuilder.AddUnitOfWork<wUtilityContentContext>("Content");
        });
        services.AddScoped<IContentUow, ContentUow>();
        services.AddScoped<AuditCoreLog>();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        ....
    }

在 ContentService.cs 文件中

public class ContentServices : ContentServiceBase
{
    private readonly AuditCoreLog _auditLog;
    private readonly IContentUow _contentUow;
    public ContentServices(IContentUow contentUow, AuditCoreLog auditLog)
    {
        _contentUow = contentUow;
        _auditLog = auditLog;
    }
    public override Task<MessageResponse> CategoryInsertUpdate(CategoryInsertUpdate_Request request, ServerCallContext context)
    {
        var response = new MessageResponse() { };
        try
        {
            int _RESPONSE_EXISTED_CATE = -2;
            int _RESPONSE_NOT_EXISTS_CATE = -3;
            int _nHotOrder = 0;
            var findnHotOrder = _contentUow.Category.Where(p => p.ParentId == request.ParentId).ToList();
            if (findnHotOrder != null && findnHotOrder.Count > 0)
               _nHotOrder = findnHotOrder.Max(x => x.OrderNo).GetValueOrDefault();
            var insertCategory = _contentUow.Category.Add(new TblCategory()
            {
                    ParentId = request.ParentId,
                    Alias = request.Alias,
                    OrderNo = _nHotOrder + 1,
                    Status = request.Status,
                    CreatedDate = DateTime.Now,
                    CreatedUser = request.CreatedUser,
                    SystemId = request.SystemId,
                    ImageUrl = request.ImageUrl
            });

                _contentUow.SaveChanges(); //savechange to DB -> get id identity
                var _categoryId = insertCategory.Entity.Id;
                var insertCategoryContent_vi = _contentUow.CategoryContent.Add(new TblCategoryContent()
                {
                    CategoryId = _categoryId,
                    CategoryName = request.CategoryNameVi,
                    CategoryContent = request.CategoryContentVi,
                    LanguageId = (int)PaymentApps_PaymentLanguage.VN
                });

                var insertCategoryContent_en = _contentUow.CategoryContent.Add(new TblCategoryContent()
                {
                    CategoryId = _categoryId,
                    CategoryName = request.CategoryNameEn,
                    CategoryContent = request.CategoryContentEn,
                    LanguageId = (int)PaymentApps_PaymentLanguage.EN
                });
            _contentUow.SaveChanges();
            _contentUow.CommitTransaction();
            response.ResponseStatus = ErrorCodes.SUCCESS;
        }
        catch (Exception ex)
        {
            response.ResponseStatus = ErrorCodes.SYSTEM_ERROR;
            _contentUow.RollbackTransaction();
        }
    }
}
于 2020-12-01T02:02:36.993 回答