Search code examples
c#asp.net-coredependency-injectionentity-framework-corehangfire

Access DbContext from within partial class in EF Core


I'm building a Hangfire application and I've scaffolded the database to EF Core. Now I've created a new partial class and from within that new partial I would like to access the DbContext so I can do some ledger stuff.

But DI does not seem to work, does anyone have another solution?

File 1

public partial class Config
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string Description { get; set; }

    public bool IsUserManaged { get; set; }

    public string ConfigurationGroup { get; set; }

    public int CreatedBy { get; set; }
    public DateTime CreationTime { get; set; }
    public DateTime? LastModificationTime { get; set; }

    public int? LastModifiedBy { get; set; }
}

File 2

public partial class Config
{
    private IDbContextFactory<DataContext> _dataContext;
 
    public Config(IDbContextFactory<DataContext> dataContext)
    {
        _dataContext = dataContext
    }

    public static T GetConfigValue<T>(string key, T defaultValue, DataContext existingContext = null)
    {
        if (existingContext != null)
        {
            return GetConfigValue(existingContext, key, defaultValue);
        }
 
        return GetConfigValue(_dataContext, key, defaultValue);
    }

    public static T GetConfigValue<T>(DataContext ctx, string key)
    {
        return GetConfigValue(ctx, key, default(T));
    }

    public static T GetConfigValue<T>(DataContext ctx, string key, T defaultValue)
    {
        var value = GetRawConfigValue(ctx, key);
 
        if (String.IsNullOrEmpty(value)) 
            return defaultValue;

        try
        {
            var conv = TypeDescriptor.GetConverter(typeof(T));
            var item = (T)conv.ConvertFrom(null, CultureInfo.InvariantCulture, value);

            return item;
        }
        catch
        {
            return defaultValue;
        }
    }

    private static readonly Func<DataContext, string, string> GetRawConfigValue =
        (context, key) => (from c in context.Configs
                           where c.Name == key.ToString()
                           select c.Value).FirstOrDefault();
}

Init for the DbContext in program.cs

builder.Services.AddDbContextFactory<DataContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

The DI does work on all other places, but only not in the partial class for the EF Core model


Solution

  • Dependency injection not work for static, you could make a instance by using

            public static T GetConfigValue<T>(string key, T defaultValue, DataContext existingContext = null)
            {
                ...
    
                using (var datacontext = new DataContext())
                {
                    return GetConfigValue(dataContext, key, defaultValue);
    
                }
                
            }