Not able to update Entity framework Core relationship from Saga Event
See original GitHub issueI am unable to update the entity relationship in my instance event. I am able to create One-to-One relational row for an entry however while I come back to the state again it is not detecting the row is already exist
Here is my State Machine
public class IssueRequestStateMachine: MassTransitStateMachine<IssueRequest>
{
public CustomerIssueStateMachine()
{
InstanceState(x => x.CurrentState);
State(() => IssueTypeAndDetails);
State(() => IssueLocation);
State(() => IssueDetails);
State(() => RequestSubmit);
State(() => RequestAcknowledgement);
Event(() => NewIssueTypeAndDetailsSubmitted,
cfg => cfg.CorrelateById(instance => instance.UserId,
dtocontext => dtocontext.Message.UserId)
.SelectId(dtocontext =>
{
return NewId.NextGuid();
}));
Event(() => IssueTypeAcceptedAtIssueLocation,
cfg => cfg.CorrelateById(dtocontext => dtocontext.Message.CorrelationId));
Event(() => IssueLocationAcceptedAtIssueDetails,
cfg => cfg.CorrelateById(dtocontext => dtocontext.Message.CorrelationId));
Event(() => RequestSubmittedToBackend,
cfg => cfg.CorrelateById(dtocontext => dtocontext.Message.CorrelationId));
Event(() => BackendAcknowledged,
cfg => cfg.CorrelateById(dtocontext => dtocontext.Message.CorrelationId));
Initially(
When(NewIssueTypeAndDetailsSubmitted)
.Then(context =>
{
context.Instance.FirstName = context.Data.FirstName;
context.Instance.LastName = context.Data.LastName;
})
.TransitionTo(IssueTypeAndDetails));
During(IssueTypeAndDetails,
When(IssueTypeAcceptedAtIssueLocation)
.Then(context => {
//This is creating new Issue Location every time when I come back to the state, this expected to be updated
//If I come back to this state
context.Instance.IssueLocation = new IssueLocation(context.Instance.CorrelationId)
{
State = context.Data.State,
PostCode = context.Data.PostCode
};
}).TransitionTo(IssueLocation));
During(IssueLocation,
When(IssueLocationAcceptedAtIssueDetails)
.Then(context => {
context.Instance.IssueDetailsText = context.Data.IssueDetailsText;
}).TransitionTo(IssueDetails));
During(IssueDetails,
When(IssueDetailsAcceptedAtRequestorContact)
.Then(context =>
{
context.Instance.Email = context.Data.Email;
}).TransitionTo(RequestSubmit));
During(RequestSubmit,
When(BackendAcknowledged)
.Then(context =>
{
context.Instance.AcknowledgementDate = context.Data.AcknowledgementDate;
}).TransitionTo(RequestAcknowledgement));
}
public State IssueTypeAndDetails { get; set; }
public State IssueLocation { get; set; }
public State IssueDetails { get; set; }
public State RequestSubmit { get; set; }
public State RequestAcknowledgement { get; set; }
public Event<IssueTypeAndDetailsDTO> NewIssueTypeAndDetailsSubmitted { get; set; }
public Event<IssueLocationDTO> IssueTypeAcceptedAtIssueLocation { get; set; }
public Event<IssueDetailsDTO> IssueLocationAcceptedAtIssueDetails { get; set; }
public Event<RequestSubmitDTO> RequestSubmittedToBackend { get; set; }
public Event<RequestAcknowledgementDTO> BackendAcknowledged { get; set; }
public Event Back { get; set; }
public Event Cancel { get; set; }
}
Saga Instance
public class IssueRequest : SagaStateMachineInstance
{
public IssueRequest(Guid correlationId):base(correlationId)
{
CorrelationId = correlationId;
}
public static IssueRequest Instance()
{
return new IssueRequest();
}
public IssueRequest():base(NewId.NextGuid())
{
}
[Required]
public string CurrentState { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public IssueLocation IssueLocation { get; set; }
public string IssueDetailsText { get; set; }
public string Email { get; set; }
public string ApplicationNumber { get; set; }
public DateTime ApplicationDate { get; set; }
public DateTime AcknowledgementDate { get; set; }
}
public class IssueLocation
{
public IssueLocation()
{
}
public IssueLocation(Guid correlationId) : base(correlationId)
{
}
public Guid CorrelationId { get; set; }
public string PostCode { get; set; }
public string State { get; set; }
}
Saga Instance Mapping
public class IssueRequestMapping : IEntityTypeConfiguration<IssueRequest>
{
public IssueRequestMapping()
{
}
public void Configure(EntityTypeBuilder<IssueRequest> entityTypeBuilder)
{
entityTypeBuilder.Property(x => x.CorrelationId);
entityTypeBuilder.Property(x => x.CurrentState).HasMaxLength(250).IsRequired();
entityTypeBuilder.HasOne(a => a.IssueLocation).WithOne().HasForeignKey<IssueLocation>(a => a.CorrelationId);
}
}
Db Context
public class IssueRequestDbContext : SagaDbContext<IssueRequest, IssueRequestMapping>
{
public IssueRequestDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Ignore<ValidationResult>();
modelBuilder.Entity<IssueRequest>().ToTable("IssueRequest");
modelBuilder.Entity<IssueLocation>().ToTable("IssueLocation").HasKey(e => e.CorrelationId);
base.OnModelCreating(modelBuilder);
}
public DbSet<IssueRequest> IssueRequest { get; set; }
}
Above code I need to update IssueLocation instead of adding new record(at IssueTypeAcceptedAtIssueLocation event). First time it is working but when I come back to the same state again that’s where it is trying to create new record. Since the record already existing in table it is throwing an error. Is there a right method to do it or is it kind of an issue out side of State Machine
Issue Analytics
- State:
- Created 6 years ago
- Comments:20 (10 by maintainers)
Top Results From Across the Web
Not able to update Entity framework Core relationship from ...
I am unable to update the entity relationship in my saga instance event. I am able to create One-to-One row for an entry...
Read more >Changing Foreign Keys and Navigations - EF Core
How to change relationships between entities by manipulating foreign keys and navigations.
Read more >Entity Framework
Once the class map and associated DbContext class have been created, the saga repository can be configured with the saga registration, which is...
Read more >Pattern: Microservice Architecture
A microservice can access a shared database where it contains all entities and its relationship. The object representation can be present if necessary...
Read more >Bulletin of the Atomic Scientists
Arnold, although an air-power man to the core, was not greatly cheered by the ... to admit that their warring power is too...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Can you elborate this part of code ? I am using .net Core and Saga state machinbe dont know how to inject DB Context to it.
I think the issue is that EF Core does not implicit object tree loading and has no lazy loading. A PR would be nice…