4

I am writing an ASP.NET Core MVC application. I am using Entity Framework. I have a data access layer where I have added my edmx from the database I designed. The MVC project is referencing this data access project.

When I try to access something from DB, this error occurs:

An unhandled exception occurred while processing the request.

InvalidOperationException: No connection string named 'DefaultConnection' could be found in the application config file.

System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()

I have set up my connection string in both data access project and Core project. But still getting this error.

This is the appsetting.json:

{
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "**My DB Details**"
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

Any idea what's going on?

2
  • 4
    You tagged entity-framework-6. Are you actually using EF 6, or are you using EF Core? Nov 26, 2018 at 19:43
  • 1
    Entity Framework is in DataAccess project which is a .NET framework class library. Yes it is using EF 6. Nov 26, 2018 at 19:47

4 Answers 4

3

It's not that straight forward when you have a .NET Core web project and it's backed by a EF6 project as class library.

In order to get things working, you might have to:

  • Reference full framework in .NET Core web project
  • Provide the connection string using Factory pattern to the DbContext
  • Share the connection string in both projects, maybe via environment variables

Manually creating a web.config in the .NET Core web project is bad as it doesn't have one until you publish. The web.config would be created by then based on the appsetting.

https://learn.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-2.1 is the documentation on how to get the .NET Core and EF6 talking.

1
  • I tried that and now I am getting Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config. I am gonna try using EF core in a separate solution and see how to handle this. Nov 27, 2018 at 4:57
1

Since you are using EF 6 in a .NET Framework library, then it's probably looking in the web.config. So put it in the web.config as described here:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DefaultConnection"
         connectionString="**My DB Details**" />
  </connectionStrings>
</configuration>

Previous answer (for purely EF Core):

Connection strings in .NET Core should look like this:

"ConnectionStrings": {
    "DefaultConnection": "**My DB Details**"
}

Not the "Data" node that you have now.

8
  • This is the setting I had initially. Even this is not working. Nov 26, 2018 at 19:48
  • I updated my answer. If it's in a .NET Framework library, then it's probably looking in the web.config for it. Nov 26, 2018 at 19:52
  • The .net class library also has a connection string added as I've mentioned in the question. Nov 26, 2018 at 20:01
  • It needs to be added to the web.config for the main MVC project. That's where it's looking. Nov 26, 2018 at 20:03
  • 1
    That's not entirely true. If you're hosting in IIS on Windows, it's still used. Are you hosting in IIS? Nov 26, 2018 at 20:10
-2

The appjson.config is wrong do this

"ConnectionStrings": {
      "DefaultConnection": "your connection string"
    }

Remove the Data tag and it should work.

-3

I had to compromise because of lack of time. I am now using EF core in the ASP.NET Core project and it's working fine. Removed the DataAccess Layer. Will use it once all the bugs with .NET Core are resolved.

1
  • Well, newer version don't have these issues. I am using .NET Core 3.1 with EF Core. Jun 27, 2020 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.