Fix EF core store

Entities were no longer updated due to the new short-lived DbContext approach
This commit is contained in:
Sipke Schoorstra 2021-01-31 12:51:05 +01:00
parent f5cc185f70
commit e6a68a0476
7 changed files with 48 additions and 15 deletions

View file

@ -11,6 +11,7 @@ namespace Elsa.Mapping
public CloningProfile()
{
CreateMap<WorkflowDefinition, WorkflowDefinition>();
CreateMap<WorkflowInstance, WorkflowInstance>();
}
}
}

View file

@ -1,5 +1,6 @@
using System;
using System.Linq.Expressions;
using AutoMapper;
using Elsa.Models;
using Elsa.Persistence.Specifications;
using Microsoft.EntityFrameworkCore;
@ -8,7 +9,7 @@ namespace Elsa.Persistence.EntityFramework.Core.Stores
{
public class EntityFrameworkBookmarkStore : EntityFrameworkStore<Bookmark>, IBookmarkStore
{
public EntityFrameworkBookmarkStore(IDbContextFactory<ElsaContext> dbContextFactory) : base(dbContextFactory)
public EntityFrameworkBookmarkStore(IDbContextFactory<ElsaContext> dbContextFactory, IMapper mapper) : base(dbContextFactory, mapper)
{
}

View file

@ -4,6 +4,7 @@ using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using Elsa.Models;
using Elsa.Persistence.Specifications;
using Microsoft.EntityFrameworkCore;
@ -12,10 +13,12 @@ namespace Elsa.Persistence.EntityFramework.Core.Stores
{
public abstract class EntityFrameworkStore<T> : IStore<T> where T : class, IEntity
{
private readonly IMapper _mapper;
private readonly SemaphoreSlim _semaphore = new(1);
protected EntityFrameworkStore(IDbContextFactory<ElsaContext> dbContextFactory)
protected EntityFrameworkStore(IDbContextFactory<ElsaContext> dbContextFactory, IMapper mapper)
{
_mapper = mapper;
DbContextFactory = dbContextFactory;
}
@ -32,10 +35,21 @@ namespace Elsa.Persistence.EntityFramework.Core.Stores
var dbSet = dbContext.Set<T>();
var existingEntity = await dbSet.FindAsync(new object[] { entity.Id }, cancellationToken);
if (existingEntity == null!)
if (existingEntity == null)
{
await dbSet.AddAsync(entity, cancellationToken);
OnSaving(dbContext, entity);
existingEntity = entity;
}
else
{
// Can't use the approach on the next line because we explicitly ignore certain properties (in order for them to be stored in the Data shadow property).
// dbContext.Entry(existingEntity).CurrentValues.SetValues(entity);
// Therefore using AutoMapper to copy properties instead.
existingEntity = _mapper.Map(entity, existingEntity);
}
OnSaving(dbContext, existingEntity);
}, cancellationToken);
}
finally

View file

@ -1,5 +1,6 @@
using System;
using System.Linq.Expressions;
using AutoMapper;
using Elsa.Models;
using Elsa.Persistence.Specifications;
using Elsa.Serialization;
@ -12,7 +13,7 @@ namespace Elsa.Persistence.EntityFramework.Core.Stores
{
private readonly IContentSerializer _contentSerializer;
public EntityFrameworkWorkflowDefinitionStore(IDbContextFactory<ElsaContext> dbContextFactory, IContentSerializer contentSerializer) : base(dbContextFactory)
public EntityFrameworkWorkflowDefinitionStore(IDbContextFactory<ElsaContext> dbContextFactory, IMapper mapper, IContentSerializer contentSerializer) : base(dbContextFactory, mapper)
{
_contentSerializer = contentSerializer;
}

View file

@ -1,5 +1,6 @@
using System;
using System.Linq.Expressions;
using AutoMapper;
using Elsa.Models;
using Elsa.Persistence.Specifications;
using Microsoft.EntityFrameworkCore;
@ -8,7 +9,7 @@ namespace Elsa.Persistence.EntityFramework.Core.Stores
{
public class EntityFrameworkWorkflowExecutionLogRecordStore : EntityFrameworkStore<WorkflowExecutionLogRecord>, IWorkflowExecutionLogStore
{
public EntityFrameworkWorkflowExecutionLogRecordStore(IDbContextFactory<ElsaContext> dbContextFactory) : base(dbContextFactory)
public EntityFrameworkWorkflowExecutionLogRecordStore(IDbContextFactory<ElsaContext> dbContextFactory, IMapper mapper) : base(dbContextFactory, mapper)
{
}

View file

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using AutoMapper;
using Elsa.Models;
using Elsa.Persistence.Specifications;
using Elsa.Serialization;
@ -13,7 +13,7 @@ namespace Elsa.Persistence.EntityFramework.Core.Stores
{
private readonly IContentSerializer _contentSerializer;
public EntityFrameworkWorkflowInstanceStore(IDbContextFactory<ElsaContext> dbContextFactory, IContentSerializer contentSerializer) : base(dbContextFactory)
public EntityFrameworkWorkflowInstanceStore(IDbContextFactory<ElsaContext> dbContextFactory, IMapper mapper, IContentSerializer contentSerializer) : base(dbContextFactory, mapper)
{
_contentSerializer = contentSerializer;
}
@ -69,7 +69,7 @@ namespace Elsa.Persistence.EntityFramework.Core.Stores
entity.ActivityOutput = data.ActivityOutput;
entity.BlockingActivities = data.BlockingActivities;
entity.ScheduledActivities = data.ScheduledActivities;
entity.Scopes = data.Scopes ?? new Stack<string>();
entity.Scopes = data.Scopes;
entity.Fault = data.Fault;
entity.CurrentActivity = data.CurrentActivity;
}

View file

@ -25,7 +25,8 @@ namespace Elsa.Server.Api.Endpoints.WorkflowInstances
}
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(WorkflowInstance))]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status202Accepted)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[SwaggerOperation(
@ -41,9 +42,19 @@ namespace Elsa.Server.Api.Endpoints.WorkflowInstances
if (workflowInstance == null)
return NotFound();
workflowInstance = options?.RunImmediately == true
? await _reviver.ReviveAndRunAsync(workflowInstance, cancellationToken)
: await _reviver.ReviveAndQueueAsync(workflowInstance, cancellationToken);
if (options?.RunImmediately == false)
{
workflowInstance = await _reviver.ReviveAndQueueAsync(workflowInstance, cancellationToken);
var model = new
{
WorkflowInstanceId = workflowInstance.Id,
};
return Accepted(model);
}
workflowInstance = await _reviver.ReviveAndRunAsync(workflowInstance, cancellationToken);
if (workflowInstance.WorkflowStatus == WorkflowStatus.Faulted)
return StatusCode(500, new
@ -52,7 +63,11 @@ namespace Elsa.Server.Api.Endpoints.WorkflowInstances
Fault = workflowInstance.Fault
});
return Response.HasStarted ? (IActionResult) new EmptyResult() : Ok(workflowInstance);
return Response.HasStarted ? (IActionResult) new EmptyResult() : Ok(new
{
WorkflowInstanceId = workflowInstance.Id,
WorkflowStatus = workflowInstance.WorkflowStatus
});
}
}