Archive for category Entity Framework

Unable to find the requested .Net Framework Data Provider. It may not be installed.

While moving a web application to a new web server, an application which used Entity Framework returned this message when loading:

ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.

   System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName) +959971

   System.Data.EntityClient.EntityConnection.GetFactory(String providerString) +26

 

And this message also appeared a bit lower on the yellow screen of death:

ArgumentException: The specified store provider cannot be found in the configuration, or is not valid.

 

The problem was with the DbProviderFactories elements in the machine.config file – it didn’t have the correct drivers installed. To fix it, I opened Notepad (Visual Studio wasn’t installed on the server) as an administrator because the file wouldn’t let me save it otherwise. I scrolled down the <system.data> section, which initially looked like this:

<system.data>

<DbProviderFactories />

</system.data>

 

I replaced it with the section below which I pulled from my local machine. I have left in all the libraries in case that helps, though in our environment, we didn’t use the SQL Server CE drivers:

<system.data>

             <DbProviderFactories>

<add name=”ODP.NET, Managed Driver” invariant=”Oracle.ManagedDataAccess.Client” description=”Oracle Data Provider for .NET, Managed Driver” type=”Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342″/>

<add name=”ODP.NET, Unmanaged Driver” invariant=”Oracle.DataAccess.Client” description=”Oracle Data Provider for .NET, Unmanaged Driver” type=”Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342″/>

<add name=”Microsoft SQL Server Compact Data Provider 4.0″ invariant=”System.Data.SqlServerCe.4.0″ description=”.NET Framework Data Provider for Microsoft SQL Server Compact” type=”System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91″/>

<add name=”Microsoft SQL Server Compact Data Provider” invariant=”System.Data.SqlServerCe.3.5″ description=”.NET Framework Data Provider for Microsoft SQL Server Compact” type=”System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91″/>

               </DbProviderFactories>

</system.data>

Advertisements

Leave a comment

Entity Framework error: ‘The underlying provider failed on Open’

I was using the Oracle.ManagedDataAccess DLL to reference an Oracle database  using Entity Framework. However, attempting this was returning the error “The underlying provider failed on Open” when my object list was being populated:

List<COASTER> coasters = context.COASTER.Where(c => c.PARK== "Cedar Point").ToList();

The issue came down to my connection string format. When trying to use the normal format that we’ve come to know and love, I’d encounter the problem. The format I’m talking about is:

metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string='DATA SOURCE=ScottServer;PASSWORD=mypw;USER ID=scott'

However, when I switched to the format that you see in the tnsnames.ora file, I had better results. So good, in fact, that it actually worked:

metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string='Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my-server)(PORT = 1521))(CONNECT_DATA=(SERVICE_NAME=testdb.www.wunderhund.com)));User Id=scott;Password=mypw;'

Leave a comment

Unable to load the specified metadata resource

I ran into this dastardly error while working on a utility to mass-update connection strings: Unable to load the specified metadata resource.

The problem was the Entity Framework connection string. Take this connection string, which uses an Oracle format:

metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string='Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle-host)(PORT = 1521))(CONNECT_DATA=(SERVICE_NAME=server-name)));User Id=Snoopy;Password=CharlieBrown;'

In three different places, you see the string “Models.Model1”. This is where my attempt went wrong. Different applications have different model names, so while this application has “Models.Model1”, another may have a different, equally creative name like “Models.Model2”. Once I corrected this, my connection worked.

Leave a comment

Encrypting the connection string for Entity Framework

I needed to encrypt the connection string that Entity Framework uses for its magic. So I encrypted the value in the “connectionString” attribute:

<connectionStrings>
   <add name="ConnString" connectionString="x4eAeGmvVTwzSOE2js7oWjQgdf" 
      providerName="System.Data.EntityClient" />
</connectionStrings>

I then updated the class in my application that is derived from the DbContext class, decrypting (using the creatively named “ScottsDecryption” (and no, that’s not the actual name)) the string in the call to “base”:

public partial class Entities : DbContext {
   public Entities()

       : base(ScottsDecryption(ConfigurationManager.ConnectionStrings["Entities"].ConnectionString)) { }
}

By the way, I received a ‘Keyword not supported: “data source”‘ error when first trying to get this working. I needed to replace the “&quot;” entries with actual single quote marks in the connection string, as described in this Stack Overflow post. So I had to change this connection string:

metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.SRModel.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string=&quot;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=blah)(PORT = 1234))(CONNECT_DATA=(SERVICE_NAME=scott.no.way)));User Id=MyID;Password=nope;&quot;

To this:

metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.SRModel.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string='Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=blah)(PORT = 1234))(CONNECT_DATA=(SERVICE_NAME=scott.no.way)));User Id=MyID;Password=nope;'

Leave a comment

The Entity Framework provider type… exception when unit testing

While merrily going about my business creating an MVC project using Entity Framework 6 against an Oracle database, I decided that unit testing my data layer would be a fun, useful thing to do. So I added a reference to my MVC project into my unit test project and away I went. But not too far because I got this error:

{System.InvalidOperationException: The Entity Framework provider type ‘Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342’ registered in the application config file for the ADO.NET provider with invariant name ‘Oracle.ManagedDataAccess.Client’ could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application.

This was thankfully an easy issue to correct. In my unit test project, I added references to the Oracle.ManagedDataAccess and Oracle.ManagedDataAccess.EntityFramework DLLs, and I was testing like a madman.

Leave a comment

MetadataException: Unable to load the specified metadata resource

Using Entity Framework, in my context class, I had defined the constructor which pointed to a connection string:

public class EFTestContext : DbContext {
    public EFTestContext()
        : base("name=Entities") {
    }
}

When I attempted to run the application, I got this message:

 MetadataException: Unable to load the specified metadata resource

Turns out, the connection string was wrong. I had copied the constructor code over from a different project, but when I created the connection string in the web.config, I named it something other than “Entities”. Once I changed the connection string in the constructor, it was good:

: base("name=RightConnString") {

1 Comment

DateTime out-of-range error in Entity Framework

Trying to add a new object to my context in MVC / Entity Framework, I ran into this error:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

I was, in fact, just adding a minimum date to my property, so I didn’t see the problem:

using (UsersContext db = new UsersContext()) {
  db.UserProfiles.Add(new UserProfile { UserName = model.UserName, 
    DateLastLoggedIn = DateTime.MinValue });
  db.SaveChanges();
}

Through the miracle of Stack Overflow, I discovered that I needed to set a DateTime greater than the minimum date. So I picked an arbitrary date

db.UserProfiles.Add(new UserProfile { UserName = model.UserName, 
DateLastLoggedIn = new DateTime(1982, 1, 1) });

I chose 1982 because that is when “I Ran” by “Flock of Seagulls” was released. But choose the release date for your favorite song,  even if, for some reason, it’s by Miley Cyrus, and you should be OK.

Leave a comment