本文介绍了如何在 .NETCore 3.1 & 中创建 DbContextFactory开拓者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循有关如何设置 EF Core 以在 Blazor & 中安全工作的指南..NET 核心 3.1.MS 文档在这里:https://docs.microsoft.com/en-us/aspnet/core/blazor/blazor-server-ef-core?view=aspnetcore-3.1

I'm following guidelines in how to setup EF Core to work safely in Blazor & .NET Core 3.1.The MS documentation is here: https://docs.microsoft.com/en-us/aspnet/core/blazor/blazor-server-ef-core?view=aspnetcore-3.1

在说明中,建议是创建一个 DbContextFactory,用于在每个服务中创建一个 dbcontext.在 Blazor 世界中一切都有意义,但代码不会编译为AddDbContextFactory 不存在.如果在 .Net Core 3.1/EF Core 3 中有另一种方法可以做到 - 我看不到它.

In the instructions, advice is to create a DbContextFactory which is used to create a dbcontext in each service. All makes sense in the Blazor world, but the code won't compile asAddDbContextFactory does not exist. If there's another way to do it in .Net Core 3.1/ EF Core 3 - I can't see it.

services.AddDbContextFactory<ContactContext>(opt =>
    opt.UseSqlite($"Data Source={nameof(ContactContext.ContactsDb)}.db")
    .EnableSensitiveDataLogging());

推荐答案

我发现 此扩展方法,Microsoft 文档页面在其示例 github 项目中使用:

I found this extension method that the Microsoft docs page is using in its sample github project:

        public static IServiceCollection AddDbContextFactory<TContext>(
            this IServiceCollection collection,
            Action<DbContextOptionsBuilder> optionsAction = null,
            ServiceLifetime contextAndOptionsLifetime = ServiceLifetime.Singleton)
            where TContext : DbContext
        {
            // instantiate with the correctly scoped provider
            collection.Add(new ServiceDescriptor(
                typeof(IDbContextFactory<TContext>),
                sp => new DbContextFactory<TContext>(sp),
                contextAndOptionsLifetime));

            // dynamically run the builder on each request
            collection.Add(new ServiceDescriptor(
                typeof(DbContextOptions<TContext>),
                sp => GetOptions<TContext>(optionsAction, sp),
                contextAndOptionsLifetime));

            return collection;
        }

而工厂类是 这里:

    public class DbContextFactory<TContext> 
        : IDbContextFactory<TContext> where TContext : DbContext
    {
        private readonly IServiceProvider provider;

        public DbContextFactory(IServiceProvider provider)
        {
            this.provider = provider;
        }

        public TContext CreateDbContext()
        {
            if (provider == null)
            {
                throw new InvalidOperationException(
                    $"You must configure an instance of IServiceProvider");
            }

            return ActivatorUtilities.CreateInstance<TContext>(provider);
        }
    }

GetOptions 方法:

private static DbContextOptions<TContext> 
    GetOptions<TContext>(Action<DbContextOptionsBuilder> action,
    IServiceProvider sp = null) where TContext: DbContext 
{
    var optionsBuilder = new DbContextOptionsBuilder < TContext > ();
    if (sp != null) 
    {
        optionsBuilder.UseApplicationServiceProvider(sp);
    }
    action?.Invoke(optionsBuilder);
    return optionsBuilder.Options;
}

这篇关于如何在 .NETCore 3.1 &amp; 中创建 DbContextFactory开拓者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 17:04