eventflow / EventFlow

Async/await first CQRS+ES and DDD framework for .NET
https://docs.geteventflow.net/
Other
2.33k stars 443 forks source link

EFCore 6 support (Missing method exception) #909

Closed AlsenDev closed 1 year ago

AlsenDev commented 2 years ago

I get this exception when trying to call PublishAsync() from the CommandBus. It seems to be a problem that occurs when using EFCore 6. I got this exception while using EFCore 6.0.0-preview.7 with the newest version of EventFlow, but I get the same error on RC 2.

System.MissingMethodException: Method not found: 'Microsoft.EntityFrameworkCore.Metadata.IEntityType Microsoft.EntityFrameworkCore.ModelExtensions.FindEntityType(Microsoft.EntityFrameworkCore.Metadata.IModel, System.Type)'.
   at EventFlow.EntityFramework.ReadStores.EntityFrameworkReadModelStore`2.EntityDescriptor..ctor(DbContext context, IApplyQueryableConfiguration`1 queryableConfiguration)
   at EventFlow.EntityFramework.ReadStores.EntityFrameworkReadModelStore`2.<>c__DisplayClass16_0.<GetDescriptor>b__0(String s) in EventFlow.EntityFramework.dll:token 0x60000a7+0x0
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) in System.Collections.Concurrent.dll:token 0x60000d3+0x10
   at EventFlow.EntityFramework.ReadStores.EntityFrameworkReadModelStore`2.GetDescriptor(DbContext context, IApplyQueryableConfiguration`1 queryableConfiguration) in EventFlow.EntityFramework.dll:token 0x600003d+0x35
   at EventFlow.EntityFramework.ReadStores.EntityFrameworkReadModelStore`2.GetAsync(TDbContext dbContext, String id, CancellationToken cancellationToken, Boolean tracking) in EventFlow.EntityFramework.dll:token 0x600003a+0x0
   at EventFlow.EntityFramework.ReadStores.EntityFrameworkReadModelStore`2.UpdateReadModelAsync(TDbContext dbContext, IReadModelContextFactory readModelContextFactory, Func`5 updateReadModel, CancellationToken cancellationToken, ReadModelUpdate readModelUpdate) in EventFlow.EntityFramework.dll:token 0x600003c+0xc6
   at EventFlow.Core.TransientFaultHandler`1.<>c__DisplayClass4_0.<<TryAsync>b__0>d.MoveNext() in EventFlow.dll:token 0x60009af+0x77
--- End of stack trace from previous location ---

I don't know if EFCore 6 support is planned yet, but I thought I at least should mention this error.

rthomson83 commented 2 years ago

I suspect this is due to framework targeting / version reference issues in the EventFlow.EntityFramework project; I found that EF Core 6 works if you register a new service with implementation of IEntityFrameworkReadModelStore using the exact same code in the repo then it works fine. Only seems to be the read store side that's affected.

jessemillerrev commented 2 years ago

Is there an update on this? I cannot get it figured out and it is blocking me hard.

jutimor commented 2 years ago

You have to target EntityFrameworks nuget packages with version 5.X.X to have it working. Seems it won't be fixed in EventFlow 0.X but only in 1.X

jessemillerrev commented 2 years ago

I solved this by implementing my own EntityFrameworkReadModel service so it will use the EF version that my project uses instead of EventFlow as was suggested by @rthomson83 above. Even implemented its own extension method for UseEntityFrameworkReadModel. This is working perfectly and when/if this gets fixed in 1.X it should be simple to point back to the EventFlow version.

nZeus commented 2 years ago

@jessemillerrev do you have an example? I'm struggling with the same issue now..

rthomson83 commented 2 years ago

@jessemillerrev do you have an example? I'm struggling with the same issue now..

Start by adding a new store like this one: https://github.com/rthomson83/eventflow-ef-core-fix/blob/main/EntityFrameworkCoreReadModelStore.cs

Then you can create a new extension method to make using it easier: https://github.com/rthomson83/eventflow-ef-core-fix/blob/main/EFReadStoreExtensions.cs

Finally use/register the service via autofac however you are using that. For me it was a module that has all the EF configuration for eventflow read store side: https://github.com/rthomson83/eventflow-ef-core-fix/blob/main/ReadStoreModule.cs

jessemillerrev commented 2 years ago

@rthomson83 I wish I had those links before I figured it all out! Thanks for posting that, hopefully others will benefit from your awesome work there.

nZeus commented 2 years ago

Thank you very much @rthomson83 for the snippets. I've just checked and your implementation indeed solves the problem. Although I'm not sure if we can use this on production :(, unfortunately. I also noticed that it generates the wrong Version number (0 instead of 1)...

Thanks again!

rthomson83 commented 2 years ago

Thank you very much @rthomson83 for the snippets. I've just checked and your implementation indeed solves the problem. Although I'm not sure if we can use this on production :(, unfortunately. I also noticed that it generates the wrong Version number (0 instead of 1)...

Thank again!

No worries, I did notice that with the version but it does increment. I've not changed anything in the store (that's the original code from the event flow repo). I'm pretty sure the version is like that because its not been set default in the read model property Version, defined with the ConcurrencyCheck attribute. If you start it at 1 it would increment from there.

github-actions[bot] commented 1 year ago

Hello there!

We hope you are doing well. We noticed that this issue has not seen any activity in the past 90 days. We consider this issue to be stale and will be closing it within the next seven days.

If you still require assistance with this issue, please feel free to reopen it or create a new issue.

Thank you for your understanding and cooperation.

Best regards, EventFlow

github-actions[bot] commented 1 year ago

Hello there!

This issue has been closed due to inactivity for seven days. If you believe this issue still needs attention, please feel free to open a new issue or comment on this one to request its reopening.

Thank you for your contribution to this repository.

Best regards, EventFlow