0

I have following app.config in my database library project

<configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>    
    <contexts>
      <!-- type:(qualified context class name, assembly ) -->
      <context type="XXX.XXX.XXX.XXX, XXX.XXX" >
        <databaseInitializer type="XXX.XXX.XXX.XXX, XXX.XXX"/>
      </context>      
    </contexts>   
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">      
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="XXX" connectionString="Data Source=XXX;Initial Catalog=Money;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

And I have a console project use this library. The problem is that it will connect to the localDB not the database I set in the library project app.config. Why?

If I set connection string in the console project's config then it works well, but it's not a good solution. Is possible to let the library project use it own config?

4
  • Can you show your DbContext class? Commented Nov 27, 2015 at 8:29
  • 1
    AFAIK , any config in class libraries are ignored at runtime. Only the start up config matters. Commented Nov 27, 2015 at 8:37
  • @AmitKumarGhosh Thx, It helps a lot. By the way may I delete the config file in the library project? Commented Nov 27, 2015 at 8:41
  • yes. You can do that. Commented Nov 27, 2015 at 8:57

1 Answer 1

1

It's always like that, the connectionstrings are in the config file of the start-up project. If you reference your database project in another project with a dll, and the connectionstring would be embedded in that project, how can you ever change it? That's the whole point of your config file in your start-up project, to configure the settings for THAT solution. If you really want to put the connectionstring in your database project (not recommended!) you can look into Embedded resources/define it as const string

You can do it like this in your DBContext class

public TestDB() : base("data source=x.x.x.x;initial catalog=DBName;
                        persist security info=True;user id=username;password=password;
                        MultipleActiveResultSets=True;App=EntityFramework")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.