How to add service names to database

I have interface

public interface ISocialService
    {
        //...
    }

and class

public class RedditService : ISocialService
{

        private readonly DbContext _context;

        public RedditService(DbContext context)
        {
            _context = context;
        }
}

I also have few other services implementing IRedditService.

This is my DbContext:

public class DbContext : IdentityDbContext
    {

        private readonly IEnumerable<ISocialService> _socialServices;
        public DbContext(DbContextOptions<CrastinatorContext> options, IEnumerable<ISocialService> socialServices) : base(options)
        {
            _socialServices = socialServices;
        }
        //...

        public DbSet<SiteName> SiteNames { get; set; }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            //...
            foreach (var site in socialservices)
            {
                builder.Entity<SiteName>()
                    .HasData(new { Name = site.GetType().Name });

            }

        }

But I can’t do this because SocialServices have dbcontext injected to constructor, so I cant inject socialservices to dbcontext. (circular dependency) The reason why I want to add Method names to db:

I want to send data to user from specific services. For example if User has RedditService preference then send him data from RedditService, if user has ServiceA, ServiceB, then send data from ServiceA and ServiceB and so on.

So I wanted to store these services names in database. Then in controller I would check user preferences: if(user has serviceA) then send data from serviceA.

Is there another way to achieve that?

TLDR: I want to add MethodNames to SiteNames table but I don’t know WHERE should I do it.

Answer

You can find all implementations of the ISocialService by reflection, so this way there is no need to inject them into the DbContext. Here’s a sample.

public interface ISocialService { }
public class FacebookService : ISocialService { }
public class RedditService : ISocialService { }

public class Program
{
    static void Main(string[] args)
    {
        var socialServices = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(s => s.GetTypes())
            .Where(p => typeof(ISocialService).IsAssignableFrom(p) && p.IsClass);

        foreach (var t in socialServices)
        {
            Console.WriteLine(t.Name);
        }
    }
}

However, I suggest redesigning the app a little bit. Otherwise, every time you add a new implementation of ISocialService or rename any existing one, you’ll need to reseed the database. I would be cautious with such couplings.