0

I have 2 projects: 1 Web api (ReportingApi) 2. Data (ReportingApi.Data)

Web Api will not connect to the class library (Data ) project without a connectionstring in web.config that is duplicate of my connectionstring in my Data project app.config.

Both have

<connectionStrings>
     <add name="ScriptsContext" connectionString="Data Source=SQLserverblah;Initial Catalog=blahdb;User ID=blah;Password=blah" providerName="System.Data.SqlClient" />
</connectionStrings>

Data project DbContext

public class ScriptsContext : DbContext
{
    public DbSet<Question> Questions { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer<ScriptsContext>(null);   
        modelBuilder.Entity<Question>().ToTable("Question", schemaName: "dbo");

    }
}
  • I want Data project to be a black box , so having a connection string in web api doesn't make sense to me –  Mar 23 '17 at 20:36
  • https://stackoverflow.com/questions/23363718/where-to-write-the-connection-string-in-app-config-or-in-web-config – William Xifaras Mar 23 '17 at 20:50
  • Hello, I know this has been awhile but did you need to install EntityFramework on your ReportingApi project? – JianYA Aug 02 '18 at 04:00

1 Answers1

0

The only config that is read is the config for the executing project. I.E. you are running the Web API website and it's config is used.

If you want Data project to be a black box, you will need to hard code the connection string or get it from a source other than configuration.

If you want to test this theory, you can remove the connection string from the Data project and still start Web API. The only reason you would need the connection string in the Data project is if you are using an ORM, like Entity Framework, that allows you to update the generated code files within Visual Studio.

Hope this helps.

Larry Dukek
  • 2,179
  • 15
  • 16