ninety7 / DbContextScope

A simple and flexible way to manage your Entity Framework DbContext instances
http://mehdi.me/ambient-dbcontext-in-ef6/
MIT License
12 stars 6 forks source link

Can it work on dotnet core 2.0 application? #6

Open Rennix09 opened 6 years ago

Rennix09 commented 6 years ago

I have register the factory and locator,

services.AddScoped<DbContextScope.EfCore.Interfaces.IAmbientDbContextLocator>(serviceProvider => new DbContextScope.EfCore.Implementations.AmbientDbContextLocator()); and the locator register as : services.AddScoped<IDbContextScopeFactory>(serviceProvider => new DbContextScopeFactory()); but when I create a scope instance use using ( var scope =_dbContextScopeFactory.Create()){} I get a exception:

"Could not load type 'System.Runtime.Remoting.Messaging.CallContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'."

what shoud i do ? thank you .

mars-boy commented 6 years ago

Hi Create own CallContext or use Async as System.Runtime.Remoting.Messaging.CallContext is not supported in Core 2.0

public static class CallContext
        {
            static ConcurrentDictionary<string, AsyncLocal<object>> dict= new 
                           ConcurrentDictionary<string, AsyncLocal<object>>();

            public static void LogicalSetData(string name, object data) =>
                dict.GetOrAdd(name, _ => new AsyncLocal<object>()).Value = data;

            public static object LogicalGetData(string name) =>
                dict.TryGetValue(name, out AsyncLocal<object> data) ? data.Value : null;
        }
djonasdev commented 4 years ago

You can use my library https://github.com/dojo90/DbContextScope

Already made a nuget package out of it.