-1

I developed a Windows Service, using Entity Framework 6.

I run the service, and when it tries to connect to database, it throws the following exception:

No connection string named 'testEntities' could be found in the application config file.

so i decided to change this code in test.Context.cs

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

into this:

public testEntities()
        : base("data source=MyLocalServer/sqlexpress;initial catalog=test;persist security info=True;user id=xxxxxx;password=xxxxxxx;MultipleActiveResultSets=True;App=EntityFramework")
    {
    }

in practice i changed the name of the entity to the connection string. But it throws the following exception:

The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception.

If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection.

To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

in the solution there are 2 projects (including wix for creating installation package). The same error is thrown if i set my project as "StartUp Project"..

Cœur
  • 37,241
  • 25
  • 195
  • 267
pasluc74669
  • 1,680
  • 2
  • 25
  • 53
  • As the error says, **make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project** – stuartd May 17 '16 at 15:27

4 Answers4

2

Add the following sections to your app.config file.

<configuration>

  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

  <connectionStrings>
    <add name="testEntities" connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=data source=MyLocalServer/sqlexpress;initial catalog=test;persist security info=True;user id=xxxxxx;password=xxxxxxx;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  /connectionStrings>

</configuration>

Where MyModel is the name of your database model.

Wicher Visser
  • 1,513
  • 12
  • 21
0

Have you checked the App.Config or Web.Config files? It is where the connection string is defined to connect the project with the database, make sure when you create the Code First to locate your server where the database is, this will edit the file and generate inside the connection string tag, which you can edit as you wish.

0

Follow code

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

says that the connection string name in app.config is testEntities

Add connection string in app.config

<configuration>
  ...

  <connectionStrings>
    <add name="testEntities" connectionString="data source=MyLocalServer/sqlexpress;initial catalog=test;persist security info=True;user id=xxxxxx;password=xxxxxxx;" providerName="System.Data.EntityClient" />
...
0

I think it is better to add connection string to app.config

Add connectionstring in app.config

But If you want your solution . for get it to work

You should not set a default connection factory in app.config

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="System.Data.SqlServerCe.4.0" />
    </parameters>
  </defaultConnectionFactory>
</entityFramework>

So You must remove defaultConnectionFactory.

Community
  • 1
  • 1
mohsen
  • 1,763
  • 3
  • 17
  • 55