RBT
RBT

Reputation: 25945

How entity framework works when providerName configuration is missing?

Here is my configuration from one of my console applications which is using entity framework:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  ....omitted few things for brevity
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
<connectionStrings>
  <add  name="codefirst" connectionString="Data Source=.;Initial Catalog=SomeCatalog;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

So here the connection string has an attribute providerName. I mention it as System.Data.SqlClient because I've already defined one provider inside entityFramework section with this name. This is ok.

But in the below configuration which is full content of App.Config file of my another console application:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="EfTestEntities" connectionString="metadata=res://*/EFTest.csdl|res://*/EFTest.ssdl|res://*/EFTest.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=EfTest;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

I mention the providerName as System.Data.EntityClient. But here I don't have any entityFramework section where I've give a provider with name System.Data.EntityClient. Still my application works. My application is able to connect to the database in the connection string and do what is directed through entity framework context.

Why the later connection string just works for EF even though Providers information is missing from the application configuration file? Is it missing or it is located somewhere else?

Upvotes: 1

Views: 1625

Answers (1)

CodingYoshi
CodingYoshi

Reputation: 27039

The first connection string is for code-first approach. That one does not specify .csdl, .ssdl and .msl files. EF will generate those files from your code-first model though. In this connection string you need to specify providerName="System.Data.SqlClient".

In the 2nd application you have, the connection string is for database first approach. That one, as you can see, specifies .csdl, .ssdl and .msl files. It also has this in the connection string: provider=System.Data.SqlClient and providerName="System.Data.EntityClient". Therefore, it knows to use EntityClient as the data provider for EF, and SqlClient as the data provider for SQL Server.

Therefore, it is all there. But they are different for code-first and database first.

Upvotes: 1

Related Questions