DbContext finds Connection String in Web Project, but not in Console App project

2

I have a Class Library project that contains the context, configuration files, and database access classes. Its reference in an Asp.Net MVC project works normally, it accesses the data and etc, but in a project of type Console Application the context does not "load" the Connection String which is in App.config , being it the same as Web.config .

Context class:

 public class SiteContext : DbContext
 {
     public SiteContext () : base("SiteBanco")
     {
         Configuration.LazyLoadingEnabled = false;
         Configuration.AutoDetectChangesEnabled = false;
     }
  }

Web.config and App.config

  

<connectionStrings> <add name="SiteBanco" connectionString="Data Source=SERVER-TEST\SQLSERVER;Initial Catalog=BaseTeste;Integrated Security=False;User ID=***;Password=*****;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" providerName="System.Data.SqlClient" /> </connectionStrings>

    
asked by anonymous 29.12.2016 / 19:27

2 answers

1

Force it to search for the given name of your ConnectionStrings like this: name=SiteBanco , this will serve both app.config and web.config :

Final code:

public class SiteContext : DbContext
{
     public SiteContext () : base("name=SiteBanco")
     {
         Configuration.LazyLoadingEnabled = false;
         Configuration.AutoDetectChangesEnabled = false;
     }
}

References:

29.12.2016 / 23:38
0

Try this:

    public SiteContext ()
        : base("SiteBanco", throwIfV1Schema: false)
    {
     Configuration.LazyLoadingEnabled = false;
     Configuration.AutoDetectChangesEnabled = false;
    }
    
29.12.2016 / 23:52