Error 500 adding Entity Framework to web.config on asp.net web api - entity-framework

In an ASP.net 4.5 Web Api, locally and in production environments is working fine.
I added the Nuget package Entity framework 6.1.3 to the solution. Without adding model. Locally with EF it is working, but when I publish to production, it throws "error 500".
EF add lines to the web.config:
<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>
In production, when I remove these lines, it works fine (of course without EF).
I tried adding a model then cause the API throws 500.
Is there any way to use EF in this hosting?
Is a problem in the web.config or in the hosting? The api is in a second level directory of a MVC web app.
pd: The hosting uses Managed Pipeline Mode: Integrated.
More info:
When i GET from browser http://max-prueba.somee.com/api/Position/41
it response a status 500 and Server Error
500 - Internal server error. There is a problem with the resource you
are looking for, and it cannot be displayed.
Next step i add line to web config to view description of the error
<customErrors mode="Off"/>
But the response is the same.

Related

EF6 and MSTest in TeamCity: Entity Framework providers must inherit from this class and the 'Instance' member must return the singleton instance

I've run into some issues with MSTest when they're run inside teamcity. When I run them inside VisualStudio on local machine, everything works good.
[08:05:21][ProjName.Data.Tests.HistoryTableHandlingTest.HistoryProgressTest] Class Initialization method ProjName.Data.Tests.HistoryTableHandlingTest.Init threw exception. System.InvalidOperationException: System.InvalidOperationException: The 'Instance' member of the Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' did not return an object that inherits from 'System.Data.Entity.Core.Common.DbProviderServices'. Entity Framework providers must inherit from this class and the 'Instance' member must return the singleton instance of the provider. This may be because the provider does not support Entity Framework 6 or later; see http://go.microsoft.com/fwlink/?LinkId=260882 for more information..
Here's my app.config for MSTest project:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Another thing that I'd like to mention is, that I don't use EntityFramework library from NuGet, but it's clone EntityFramework.HierarchyId (it can be found in NuGet as well).
After I rewrote all tests to NUnit, I got the same results.
But it seems, that the main reason of this behavior was, that on the build agent MS Sql Server Express wasn't installed properly.
When I re-installed latest version of SQL Express, everything started to work as a sharm. It seems that system was missing some drivers or libraries for LocalDB.
I hope that this will help someone else.

Oracle ODAC 12c Release 3 32-bit beta supports EF 6.x?

I have Windows 8.1 64-bit with Visual Studio 2013. I've installed the latest Oracle ODAC 12c Release 3 32-bit beta which claims to supports EF 6. When I add the ADO.NET Entity Framework to my project and choose my Oracle data connection, it doesn't allow me to select the Entity Framework 6.0 version. It has Entity Framework 5.x selected and version 6.x is greyed out. It says 'An Entity Framework database provider compatible with the latest version of the Entity Framework could not be found for your data connection'. Why is that?
I did the following to make it work :-
First Installing ODAC 12c Release 3 which includes support for Entity Framework 6 Code First and Code First Migrations; NuGet, .NET Framework 4.5.2; and ODP.NET, Managed Driver XML DB. As per
http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.html
Adding two references , to my project references and they are :
Oracle.ManagedDataAccess.dll
Oracle.ManagedDataAccess.EntityFramework.dll
Installing EF6.1.1 using NuGet by running the following command in Package Manager Console( you can enter it by Tools->NuGet Package Manager -> Package Manager Console):
Install-Package EntityFramework -Version 6.1.1
And modify your web.config or web.config to use Oracle.ManagedDataAccess , by adding Provider and a valid connection string eg :
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="Oracle.ManagedDataAccess.Client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<entityFramework>
<contexts>
<context type="App.Context.Default, App.Context">
<databaseInitializer type="MyProject.Context.Config.ContextInitializer, MyProject.Context" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="Default" providerName="Oracle.ManagedDataAccess.Client" connectionString="DATA SOURCE=XE;USER ID=User" />
</connectionStrings>
Rebuild your Application as x86, and start using EF6 , you can check if it works by adding a model using ADO.Net Entity Model using Code First
Did you install from this file: ODAC121010Beta2_32bit.zip and then choose to install Oracle Developer Tools?
It is the only file on that page that includes the Oracle Developer Tools for Visual Studio which must be updated for design time EF work.
set Oracle.ManagedDataAccess.EntityFramework.dll Specific version to True and rebuild your app. And then try adding ADO.NET Entity wizard again

How to resolve Warning : The element 'entityFramework' has invalid child element 'providers'. List of possible elements expected: 'contexts'

I'm playing around with EF different workflows. Yesterday I made a new project and Entity Framework 6 was the first suggestion from Nuget so I decided to give it a try, also it is a very small project entirely for learning purposes so I guess it will be good experience to try EF 6 since I've been working mostly with Ef 5.
My Application is based on Code First approach. The structure of the solution is shown in the print screen:
The project CodeFirstClasses is meant to hold my Entites. For simplicity purposes and because I follow a tutorial I use only one class as you may see - Customer.cs. There I have :
public class RewardContext : DbContext
{
//Specify the name of the base as Rewards
public RewardContext() : base("Rewards")
{
}
//Create a database set for each data item
public DbSet<Purchase> Purchases { get; set; }
public DbSet<Customer> Customers { get; set; }
}
And the other classes - Purchase and Customer which are trivial, so I won't paste them here.
The other project as you can see is Windows Forms project with just one form and button on it. On the click event of the button I have all the logic for adding new records to my two entities hardcoded. Here is just a part of it:
//some code...
//Add the record and save it
context.Customers.Add(newCustomer);
context.Purchases.Add(newPurchase);
context.SaveChanges();
MessageBox.Show("Record Added!");
So far nothing different from what I'm used to with EF 5. I can build the project, I can run it, and everything is executed as expected. However I get this warning from the title :
Warning 1 The element 'entityFramework' has invalid child element 'providers'. List of possible elements expected: 'contexts'. And even though I'm using mostly MS SQL Server Management Studio I've noticed that I'm not able to manage my connections/databases from the IDE - Visual Studio 2012, but this was not an issue with EF 5.
My research narrowed down the possible source of problem/solution to manually changing the App.config file, but this is an area where I haven't got much experience especially when the IDE took care of it until EF 6. So I'll post both my App.config files for this solution :
The one from the CodeFirstClasses project :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<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>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
And from my TestCodeFirst project:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<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>
</configuration>
And the other possible solution that I found is : updating the xsd for "validating" EF config section in web/app.config file to recognize newly added EF6 elements which I'm also not aware of how exactly to do it.
Even though when I open the MS SQL Server Management Studio I see the database created for this application, the records are saved and generally it seems to work but yet I would like to resolve this warning and get to know how to set up my applications based on EF 6 right.
You can install the EF6 Designer for VS2012 from here and it will update the schema that validates config files.
The configuration schema was changed from version 5 to 6. As it says, the providers node was replaced with a contexts node.
The idea is that you can configure the providers individually instead of all contexts using the same provider. (This goes in tandem with being able to have multiple contexts that reside within one database. This used to be called multi-tenant but was later renamed to be more concise)

entity framework code first migrations from seperate windows class library

i have a solution that contains an MVC project and windows class library project that uses entity framework and is a data access layer. I tried to enable migrations with the following package manager console line
Enable-Migrations -ProjectName PortlandRoad.DAL -ContextTypeName PortlandRoadDBContext -Force
I try to update the database using the following line
Update-Database -Verbose -Force
if i do this with the mvc project as the startup project it works, but uses the connection string in the mvc project web.config. If i do this with the dAL project as the startup project i get the following error :
A file activation error occurred. The physical file name '\PortlandRoadDB.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
the app.config file for my DAL project is as follows :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!--<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />-->
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=PortlandRoadDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\PortlandRoadDB.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
can anybody tell me how to correct this so that the migrations work specifically for the dal project and are not dependent on the MCV project being the startup project
thanks
I've the same issue in a WPF project.
I think that you have two options.
Option 1
At startup in you windows project try to specify the correct path to your database using
AppDomain.CurrentDomain.SetData("DataDirectory","your\physical\path");
With this, you will have the same error but your migrations can still being applied to your principal database.
option 2
Find a way to replace dynamically this |DataDirectory| in your app.config file before starting your application. (.bat , or .ps or ...).
another option: The bad one.
Before running your migration change the path to you DataDirectory to a physical temporary one in your app.config (this file will help you keep up to date migrations history)
before launching your application replace the temporary file path by |DataDirectory| and change this parameter in your startup with the App.config
With this work around, if your are planning to deploy your application, you will keep track of all migrations in order to anticipate futures database modifications.
VoilĂ .
Could it be that you're using "|DataDirectory|" in the connection string in the DAL project? Since "|DataDirectory|" is an Asp.Net folder and the DAL project is not an Asp project it probably doesn't recognize that as a directory and it probably doesn't exist thing the DAL project.

Entity Framework 5 Code Migration doesn't create database

I'm trying to use Entity Framework 5 Code First.
I've created model classes and context.
After that following Microsoft instructions I've enabled code migrations.
Than I skipped running my application (during which DB should be created first time) and use Add-Migration command. Migration was generated successfully.
Update-Database call was also successfull. But I'm not seeing my DB at all. It is absent!
SQL Management Studio and Visual Studio Server Explorer show only 4 default system databases and that's all!
I also tried to launch my application - it doesn't change anything.
I'm using
public MyContext() : base("name=MyContext") { }
such type of constructor (so I need specify connection string with MyContext name.
Here is my app.config example:
<configuration>
<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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="BettyContext" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=MyDB.sdf"/>
</connectionStrings>
</configuration>
I have no idea what's going on. Seems that no one has such problem. May be I've missed some evident thing, but howbeit I'm waiting for some help.
Do you see an exception? If you don't see an exception you probably are not looking at the right database. You can try adding some data to the database with your app and then read it. If the data can be successfully added and read and you still don't see the database it would confirm that you are looking at wrong database.