簡體   English   中英

在 .net 內核、c# 中將上下文作為參數傳遞

[英]Pass a context as a parameter in .net core, c#

我有當前的情況,我有這個基本上下文,我從中繼承了其他兩個上下文

namespace AutoAttendant.Data
{
    public class BaseDbContext : DbContext
    {
        protected BaseDbContext(DbContextOptions<BaseDbContext> options)
            : base(options)
        {
        }

        protected BaseDbContext(DbContextOptions options)
            : base(options)
        {
        }

        public virtual DbSet<LockStatus> LockResult { get; set; }
    }

    public partial class AutoAttendantContext : BaseDbContext
    {
        internal AutoAttendantContext(DbContextOptions options)
            : base(options)
        {
        }

        public AutoAttendantContext(DbContextOptions<AutoAttendantContext> options)
            : base(options)
        {
        }
    }

    public partial class ReadOnlyAutoAttendantContext : BaseDbContext
    {
        internal ReadOnlyAutoAttendantContext(DbContextOptions options)
            : base(options)
        {
        }

        public ReadOnlyAutoAttendantContext(DbContextOptions<ReadOnlyAutoAttendantContext> options)
            : base(options)
        {
        }
    }
}

我想在這里使用這個上下文作為參數

namespace AutoAttendant.API.ConfigurationExtensions
{
    public static class ServiceCollectionExtension
    {
        public static IServiceCollection AddPomeloDataSourceConfiguration(this IServiceCollection services, string connectionString, Version version, **BaseDbContext context**)
        {
            services.AddDbContextPool<**context**>(options => options.UseMySql(connectionString, new MySqlServerVersion(version), op =>
            {
                op.EnableRetryOnFailure();
            }));

            return services;
        }
    }
}

但我不知道如何正確傳遞它,因為在AddDbContextPool中使用了一種類型,有什么想法嗎?

當我調用 AddPomeloDataSourceConfiguration 時,如何將其與繼承的上下文一起使用

services.AddPomeloDataSourceConfiguration(Configuration.GetConnectionString("DefaultConnection"), new Version(5, 7), AutoAttendantContext);

services.AddPomeloDataSourceConfiguration(Configuration.GetConnectionString("ReadOnlyConnection"), new Version(5, 7), ReadOnlyAutoAttendantContext);

您可以使您的上下文類型通用:

    public static IServiceCollection AddPomeloDataSourceConfiguration<TContext>(
        this IServiceCollection services, 
        string connectionString, 
        Version version)
        where TContext : BaseDbContext
    {
        services.AddDbContextPool<TContext>(options => options.UseMySql(connectionString, new MySqlServerVersion(version), op =>
        {
            op.EnableRetryOnFailure();
        }));

        return services;
    }

您可以將其作為類型參數傳遞:

public static IServiceCollection AddPomeloDataSourceConfiguration<TContext>(this IServiceCollection services, string connectionString, Version version)
{
    services.AddDbContextPool<TContext>(options => options.UseMySql(connectionString, new MySqlServerVersion(version), op =>
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM