Reputation: 405
I've been playing around with the masstransit sample from here https://github.com/MassTransit/Sample-ShoppingWeb Allthough i have updated to the latest version(3.3.5) of masstransit and everything works fine.
I want to add ShoppingCartItems to my ShoppingCart so i added it to the model and the mapping like this.
public class ShoppingCartMap :
SagaClassMapping<ShoppingCart>
{
public ShoppingCartMap()
{
Property(x => x.CurrentState)
.HasMaxLength(64);
Property(x => x.Created);
Property(x => x.Updated);
Property(x => x.UserName)
.HasMaxLength(256);
Property(x => x.ExpirationId);
Property(x => x.OrderId);
HasMany(c => c.ShoppingCartItems);
}
}
public class ShoppingCart :
SagaStateMachineInstance
{
public string CurrentState { get; set; }
public string UserName { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
/// <summary>
/// The expiration tag for the shopping cart, which is scheduled whenever
/// the cart is updated
/// </summary>
public Guid? ExpirationId { get; set; }
public Guid? OrderId { get; set; }
public Guid CorrelationId { get; set; }
public virtual List<ShoppingCartItem> ShoppingCartItems { get; set; } = new List<ShoppingCartItem>();
}
public class ShoppingCartItem
{
public Guid? Id { get; set; }
public string Name { get; set; }
public Guid? OrderId { get; set; }
}
This is run at startup:
SagaDbContextFactory sagaDbContextFactory =
() => new SagaDbContext<ShoppingCart, ShoppingCartMap>(SagaDbContextFactoryProvider.ConnectionString);
_repository = new Lazy<ISagaRepository<ShoppingCart>>(
() => new EntityFrameworkSagaRepository<ShoppingCart>(sagaDbContextFactory));
The problem i get is an error message saying the model has changed. If i drop the database and run the solution from scratch it works but i dont want to drop my entire DB every time i need to make a change in my saga class.
My plan is to build my ShoppingCart through the saga and when i reach my finished state i will use the saga context(ShoppingCart) to create and persist real orders. Maybe i am going by this all wrong and have missunderstood the whole concept of sagas? If so how would one go about sagas that have complex object graphs?
Upvotes: 1
Views: 1310
Reputation: 19640
Saga persistence just saves your saga instance objects to some tables, according to your mapping. You can use your persistence layer's own schema update tools to fix this. I do not think this has anything to do with MassTransit. For EF you can use EF code-first migrations. For NH you can use the built-in schema update. For document databases like MondoDb or RavenDb you just do nothing.
In any case, think about this as a normal database schema change task. You have to put some effort in it like in any other database schema change. For example, you need to consider migrations required to fix your existing saga when you update the schema. As for any other schema change you would need to have some scripts or code to fix this. The same applies for document databases as well although you do not need to have schema update scripts or code for every change, but at least for those that require to change existing saga documents.
Upvotes: 1