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);
source to share
No one has answered this question yet
Check out similar questions: