Prevent inheritance of incompatible configuration section in child application - entity-framework

Project A
uses entity framework 4.4.0.0 and has below entry in web.config
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
Project B
uses entity framework 6.0.0.0 and has below entry in web.config
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
Project B is hosted under Project A in IIS (Project B was virtual directory, later converted to web application)
If I access project B, it shows message in browser that there is duplicate entry of entity framework.
I commented entry in project B
<!--<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />-->
This makes Project B run, but as soon it access data from database, an exception is thrown.
An error occurred creating the configuration section handler for entityFramework: Could not load file or assembly 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies.
I cant edit Project A EF or Project B EF, as I don't have access
Any suggestions on how to deal with this?

You can do that by disallowing inheritance of the section. To do so, you can add this atribute to what shouldn't be inherited in the parent web.config: inheritInChildApplications="false"
As explained here not all elements suport it, so perhaps you have to do it in a parent, and redefined all the other things that should be inherited: How to stop inheritance of <configSections>in Web.Config This is a good solution for it: Avoid web.config inheritance in child web application using inheritInChildApplications
You can also see the 7th tip of this asp.net log: 10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides

Related

Calling Entity frameworks file from Exe, normally a part of a web service

I am working on a older webservice, which uses Entity frameworks 6 to connect to Database.
The project is split up into web-service (controller) and "repository", which operated the database.
The idea is to now create an EXE which calls the repository, hence class library, which uses Entity framework. However, here I have some settings issues with connecting to the database.
The repository's config holds connection data, but for some reason it cannot connect.
Running is a default, hence default setting (none) of my exe, I get this error:
I can debug in into
public partial class My_TestEntities : DbContext
{
public My_TestEntities()
: base("name=My_TestEntities")
{
}
It crashes at that moment.
"Exception has been thrown by the target of an invocation." - which points to something deeper that just this.
I try to copy default into my own app.config
<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>
<connectionStrings>
<add name="my_TestEntities" connectionString="metadata=res://..... stuff here..." />
</connectionStrings>
<appSettings />
This results in
"The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception."
Copying the full config from the repository, some web stuff etc results in the same
Copying the configuration from the controller results in same as well.
I tried those things as I don't know what to do. I googled and found nothing.
So maybe here I can get some good ideas
I can answer this myself.
The parent exe calling the repository needs to have Entity framework (nuget package) installed, this will add some parts automatically to the config, as above.
Next, only the connection string is needed in the config file.
Then it works

Empty the log file every time I write with Enterprise Library Logging

I have Enterprise Library Logging used in my project which is working fine, but I need only the last entry to be saved at any time. Is there any configuration for that?
Below is my current configuration.
<add name="WorkflowListener" formatter="Workflow Formatter" fileName="..\#data\logs\Workflow_log.txt" timeStampPattern="yyyy-MM-dd-HH-mm-ss" rollFileExistsBehavior="Overwrite" rollInterval="None" header="" footer="" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

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

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.

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.

PowerTools Beta 3 fails

I was trying to generate "views" to enhance the performance of my application. The application is Silverlight 4 linked with RIA services to the Server. The server application started with EF 4.1 + RIA Services.
I installed "EF Power Tools Beta 3 v0.7.0.0" and could generate my views with no problem. But I soon realized that EF 4.1. was not able to benefit from the precompiled views. EF >= 4.2 was required.
Through NuGet I saw an update for RIA Services (RIAServices.EntityFramework.4.2.0) which used EF 5. I installed and got everything working but when I try to generate views I get an exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
I've tried reinstalling PowerTools. No luck
I've downloaded source code for PowerTools and debugged the Exception. Apparently PowerTools was trying and could not find the old EF version. The LoaderExceptions property read:
{System.IO.FileNotFoundException: Could not load file or assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies.
I've investigated the PublicKeyToken and confirmed that it is the same as the EF 5 (EF 4.4 target=.NET 4) dll found in my "bin" output map. But PowerTools is looking for EF 4.1.. why?
My web.config file has this:
<sectionGroup name="system.serviceModel">
<section name="domainServices" type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" allowDefinition="MachineToApplication" requirePermission="false" />
</sectionGroup>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
and:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.4.0.0" />
</dependentAssembly>
</assemblyBinding>
I've also tested changing the PublicKeyToken in Web.config just to confirm that I get another error from PowerTools thus PowerTools seems to parse my Web.config.
Update:
I've created a new Web project and added all Model code files as links. Same connectionstrings. Now i can build views.. strange.. same references.
When I add the views to my original project the RIA services won't start. Removed views and they started again.