How to pass Connection String from .NetCore to EF Class Library

Viewed 670

Overview: I have two projects, one ASP.NetCore Web API and one EF 6.1 Class Library. The class library contains manager classes that interact with my database and return objects.

I followed this tutorial for dependency injection, but it does not address the scenario in my project where EF is in a separate class library and interaction takes place via manager classes:

https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6

I got things working in the following way (but I am not happy because I don't want to overload the connection string):

    public class AccountManager
    {
        private static string ConnectionString = LibDataEntities.ConnectionString;

        public static AccountDC AuthenticateAccount(string EmailAddress, string Password)
        {
            LibData.Model.AccountDC account = null; 

            using (var db = new LibDataEntities(ConnectionString))
            {
                var qry = (from accounts in db.Accounts
                           where accounts.IsAdmin == true
                           && accounts.Active == true
                           && accounts.Email == EmailAddress
                           && accounts.Password == Password
                           select new LibData.Model.AccountDC
                           {
                               Active = accounts.Active,
                               Email = accounts.Email,
                               FirstName = accounts.FirstName,
                               Id = accounts.Id,
                               IsAdmin = accounts.IsAdmin,
                               IsSuperAdmin = accounts.IsSuperAdmin,
                               LastName = accounts.LastName,
                               LibraryId = accounts.LibraryId,
                               Password = accounts.Password
                           }).FirstOrDefault();

                if (qry != null)
                {
                    account = new LibData.Model.AccountDC();
                    account = qry;
                }
            }

            return account;
        }
}


   public partial class LibDataEntities
   {
      public static string ConnectionString;

      public LibDataEntities(string connectionString) : base(connectionString)
      {
      }
   }

  public Startup(IHostingEnvironment env)
  {            
     // Set connection string in Manager Project
     LibData.Manager.Entities.LibDataEntities.ConnectionString = Configuration.GetConnectionString("LibDataEntities");

  }

Is it possible to interact with the class library managers WITHOUT having to overload the connection string (such as the example below)?

using (var db = new LibDataEntities())

FWIW: the reason I do not want to overload the connection string is because this class library is used by a non-asp.netcore application where the Web.Config already contains the connecting string.

Here is some new code I am trying without success (as indicated in one of my replies below:

        public void ConfigureServices(IServiceCollection services)
    {
        services.AddDistributedMemoryCache();
        services.AddSession();
        services.AddAuthorization();
        services.AddMvc(options =>
        {
            options.Filters.Add(new ExceptionFilter());
        });
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddMvcGrid();

        services.AddScoped(_ => new AccountManager());
        services.AddScoped<LibDataEntities>(_ => new LibDataEntities(Configuration.GetConnectionString("LibDataEntities")));

    }

    private readonly AccountManager _am;
    public HomeController(AccountManager am)
    {
        _am = am;
    }

From the controller:

LibData.Model.AccountDC account = _am.AuthenticateAccount(lvm.Email, lvm.Password);
0 Answers
Related