MVC SQL No Connection String Named Could Be Found in the Application Config File EF6 - sql

I have searched through a number of questions(No Connection String Named MyEntities Could Be Found In The Application Config), but most seem to deal with multiple projects in a solution, in this case I have only one project in one solution. Also this is my first MVC Project with EF6. When going to add a controller I get this error:
This is my Web.Config
And This is My Solution Explorer
I know the error references an Application Config File, but I do not have an app Config file, nor do I know where to put it or how to create it.
EDIT, Here is the code for the db context
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("name=DBConnection"){}
public System.Data.Entity.DbSet<zzz.Models.Supplier> Suppliers { get; set; }
}

Related

ASP.NET Core 3.1 get connection string in class library

I am crazy confused. I refactored my solution into two projects; a web and a DAL. I am trying to follow the principles of DI, but I don't know how to get my connection to the DAL.
Here is what I have so far:
In web project:
public void ConfigureServices(IServiceCollection services)
{
// connection string
var connectionString = new ConnectionString(Configuration.GetConnectionString("connectionstringfromappsettings"));
services.AddSingleton(connectionString);
...
}
Class in web project to get value
public sealed class ConnectionString
{
public ConnectionString(string value) => Value = value;
public string Value { get; }
}
Class in DAL repo that I'm not sure how to get configuration:
public class ContactRepo : IContactRepository
{
private readonly ConnectionString _connectionString;
public ContactRepo(ConnectionString connectionString)
{
_connectionString = connectionString;
}
...
}
Thanks for the help in advance.
Your are mapping your configuration to ConnectionString object located on your main Web app(which you called WWW project). You have a dependency from Web project to DAL class library as expected. you can not call the classes in Main app from DAL project. You may locate your ConnectionString at DAL project or some another layer which both projects have dependency towards. Also, you can directly call Configuration.GetSection(..)... when Configuration is the injected instance of IConfiguration.
I had the class ConnectionString in the WWW project.
My fault:
This post helped me walk it though a bit.
Here
I think you should research Inversion Of Control(IOC) on google. For more deep info you should look Castle Windsor and Ninject packages.

EF Core in class library and use from API project - How to pass config information to class library?

Ive setup a solution with a .net core class library project and an asp.net core web api project. To use my class library I added the interface via ConfigureServices, where DataRepo is a class in the class library.
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IDataRepo, DataRepo>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
This class library needs to get the environment-specific connection string that will be in the API project file appsettings.json. How can I pass config settings over to it? Also, I wan to add an EF core datamodel in the library project and Id rather not register a DbContext with the API project, but rather in the constructor of my library, use the connection info to pass along with the config in the DbContext. First things first, How can I get the config settings from the appsettings.json to my class library?
namespace DataLib
{
public class DataRepo : IDataRepo
{
public DataRepo()
{
}
public string GetHello()
{
return "hello from lib";
}
}
}
If your class needs a connection string, then simply give it the connection string. You can utilize the overload for AddScoped that takes an Action<T>.
First, add a constructor that takes your connection string:
public class DataRepo : IDataRepo
{
private readonly string _connectionString;
public DataRepo(string connectionString)
{
_connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
}
}
Then, in Startup.cs:
services.AddScoped<IDataRepo>(p => new DataRepo(Configuration.GetConnectionString("Foo")));
Your class should not know or care where the connection string actually comes from, which is why other approaches, such as injecting IConfiguration are wrong here.
If you only wants to share configuration between a lib project and a website project , you'd better use dependency injection to do that .
A lib project should not care about the location of the configuration file . Anytime we need a configuration in our lib project , let's say a ConnectionString , we should just ask for it .
The first to do is to add a package reference in the lib project :
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
And then inject an IConfiguration when we need it . For example , we should have a IConfiguration object injected in DataLib:
namespace DataLib
{
public class DataLib: IDataRepo
{
private readonly IConfiguration _configuration ;
public DataLib(IConfiguration config ){
this._configuration=config;
}
public string ConnectionString{
get{
return _configuration.GetConnectionString("DefaultConnection");
}
}
}
}
The IConfiguration doesn't care where is the configuration file or which kinds of arguments are passed by command-line when started . Actually , it doesn't care about anything . The DataLib just uses a simple POCO .
When we need the class lib in main project , we can simply add the service to DI container by :
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddScoped<DataLib.DataLib>(); // if you want to use DbContext , just use AddScoped<TService>()
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
That's all we have to do . The appsettings.json and other configurations will be built into an instance of IConfiguration and then be injected to the lib project automatically.
There're also other solutions , such as using linked files and so on . However , the approach above keeps the DataLib a plain POCO . This is great . As a lib is always used by a main project , we don't need to care about the configuration or how it is created . The main project will take care of it .

Use Ninject in both main and referenced projects

I have MVC4 website project and WCF project, both using Ninject.
I want to use class from WCF project in website project. I add reference to project and get both NinjectWebCommon.Start() executing (with "The static container already has a kernel associated with it!" error).
Is there way to make what I want?
Solved this using this startup in referenced project
public class Global : NinjectHttpApplication
{
protected override IKernel CreateKernel()
{
return new StandardKernel(new ServiceModule());
}
}

Stop ApplicationDbContext creating a database

Is there some way I can just stop in a .NET 4.5, MVC5 application the ApplicationDbContext from creating the authentication database for me. I'm on shared hosting so it's not an option.
Right now I'm quite happy to have two database's running, one for production and one for authentication, I will get back to sorting that out later. For now I need to be working on SEO and put the authentication database up manually.
I'm guessing it would mean re-writing this code?
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
The site is ready to go bar this issue, can this be done?
Got it!
Kind of tricked it, as long as you use the same database name it wont try and re-create it and it's all working now. SEO it is now!

How to inject repositories into a custom MembershipProvider?

It seem i can't get this to work. I made a custom MembershipProvider and i want to inject a repository inside but the [Inject] property just doesnt work.
public class PyrosphereMembershipProvider : MembershipProvider
{
[Inject]
protected IDepositoireUtilisateur DepositoireUtilisateur { get; set; }
[Inject]
protected IDepositoireProfile DepositoireProfile { get; set; }
...
I think this is because this class are created way before the MVC application is running which cause the actual problem. I am getting more problem trying to use the membershipprovider than making my own system.
About my ninject configuration i am using the App_Start directory method. All my binding are correct ive looked so that nothing to do with my bindings.
Any help is appreciated!
The ASP.NET Providers aren't designed for proper dependency injection. There are only workarounds for them.
Either you have to setup a binding for them and resolve atleast one instance before using them. E.g. by injecting an instance into the Global.asax or you have to Inject an instance manually before the first use (kernel.Inject(...)). Again most likely in the global.asax application start method.