0

I am using asp net core 2.1 and EF 6, using DB First. My connectionString in App.Config:

I have this error: An unhandled exception occurred while processing the request. InvalidOperationException: No connection string named 'DataContext' could be found in the application config file. System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()

Any help will be appreciate.

R_SH
  • 163
  • 8
  • Can you show us your DataContext ? This issue may related to the content in your DataContext, you can refer to this : https://stackoverflow.com/questions/10978017/no-connection-string-named-myapplicationentities-could-be-found-in-the-applica – LouraQ Jun 10 '20 at 09:26
  • public partial class APWebDataContext : DbContext { public APWebDataContext() : base("name=APWebDataContext") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet x{ get; set; } } } – R_SH Jun 10 '20 at 18:22

1 Answers1

0

According to the content of APWebDataContext you provided, the cause of the error is this:

public APWebDataContext() : base("name=APWebDataContext") { } 

You don't need to add name here.

Modify your APWebDataContext as follows:

public partial class APWebDataContext : DbContext 
{ 
  public APWebDataContext() : base("APWebDataContext") { } 

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  { 
    throw new UnintentionalCodeFirstException(); 
  } 

  public virtual DbSet<xxxxxx> x{ get; set;} 

} 
LouraQ
  • 6,443
  • 2
  • 6
  • 16
  • I am using DB first not code first. I found this article from microsoft which explained how to have separate project in EF6 with asp.net core. https://learn.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-2.1 still this didnt help me that much a I am using DB first and my connection string differs from another type and I cant modify that. – R_SH Jun 11 '20 at 11:49