How to pass connection string from .NetCore library to EF Class Library

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 dependency injection tutorial, but it doesn't account for the scenario in my project where EF is in a separate class library and the interaction is through the manager classes:

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

Everything works for me as follows (but I'm 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 class library managers without overloading the connection string (like example below)?

using (var db = new LibDataEntities())

      

FWIW: The reason I don't want to overload the connection string is because this class library is being used by a non-asp.netcore application where Web.Config already contains the connection string.

Here is some new code that I am trying without success (as pointed out in one of my answers 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);

      

+3
c # asp.net asp.net-core entity-framework


source to share


No one has answered this question yet

Check out similar questions:

2058
How do I get a consistent byte representation of strings in C # without manually specifying the encoding?
1273
How can I convert a byte array to a hex string and vice versa?
1266
How do I update the GUI from another thread?
774
Get property value from string using reflection in c #
747
Create Comma Separated List from IList <string> or IEnumerable <string>
721
How would you count the occurrences of a string (actually char) within a string?
685
How do I create a stream from a string?
five
Entity Framework - use dynamic Oracle provider connection string
five
How to use SQL connection string with ADO.NET Entity Data Model
0
SQL change static connection string if no connection



All Articles
Loading...
X
Show
Funny
Dev
Pics