Problem accessing EF resources from windows service - entity-framework

I have a WCF service which uses Entity Frameworks to connect to a database, all hosted in a Windows Service. The problem is that when the first message is handled to cause a database access, an exception is thrown: "Unable to resolve assembly 'MyService.exe'". Basically, when running as a windows service EF can't locate the embedded resources.
I've checked for the presence of the 3 resources, csdl, ssdl, msl using .Net Reflector in the static exe and they're there. I'm using this connection string
metadata=res:// MyService.exe/MyModel.csdl|
res:// MyService.exe/MyModel.ssdl|
res://MyService.exe/MyModel.msl; provider=...".
I added code to the Windows Service to enumerate the embedded resources in the OnStart method when the service is started (using GetExecutingAssembly(). GetManifestResourceNames()), and it shows nothing there. [of course, the win svc is installed/run with installutil + net start]. When I run a nearly identical project, linking to all the same sources including the app.config, as an interactive console app (different Main() of course), everything works fine, including the enumeration of the resources (all 3 edmx mapping files are there). The Win Svc is running under Local System.
Has anyone else had difficulties using EF in a windows service? Any ideas about whats going wrong or how to diagnose/fix it?

Try replacing "MyService.exe" with "*" like this:
<add name="MyEntities" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sqlexpress;initial catalog=DatabaseName;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

Related

The connection string in the application's configuration file does not contain the required providerName attribute

I have a WorkerRole in Azure that's connecting to an MSSQL database (also in Azure, on its own virtual machine -- in other words not an Azure SQL database). The WorkerRole is using EntityFramework (code first).
My connection string looks like this and connections from the worker role works just fine in my development environment:
<add name="MyConnectionString" connectionString="Data Source=mydatabaseserver.cloudapp.net;Initial Catalog=MyDatabase;User ID=MyUser;Password=ThePassword;" providerName="System.Data.SqlClient" />
However, the application throws the error:
The connection string MyConnectionString in the application's configuration file does not contain the required providerName attribute
As you can see, it clearly does contain the providerName attribute. I have checked the obj/Release folder to see the actual configuration file that is published to Azure, and it does contain the attribute as well.
What am I doing wrong?
Never mind. It is in fact working now.
The reason I thought it wasn't working is that there is a delay in how Azure writes Trace data to TableStorage. This led me to believe that the WorkerRole was still throwing the error after updating the connection string, even though it wasn't.

Web app with EF + database first not working on Azure

I have a ASP.NET MVC application which uses EF (v6) as data access layer. My application works fine on IIS Express and also when deployed to the server running IIS 7.5.
The problem is that I'm getting the following exception when I deploy it to Azure (Web Sites).
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
I've searched the web and I made sure my connection string starts with "metadata=" + checked my db context class' constructor to be sure it contains the correct name (in my case it's "name=PsDataEntities"))
My connection string looks like this: <add name="PsDataEntities" connectionString="metadata=res://*/PsDataModel.csdl|res://*/PsDataModel.ssdl|res://*/PsDataModel.msl;
provider=System.Data.SqlClient;
provider connection string="data source=SERVER_NAME;initial catalog=DB_NAME;user id=UID;password=PWD;
MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Anybody had the same problems?
Any help is appreciated
I did a few more tests (like changing metadata in the connection string to use fully qualified names instead of '*') without success, then I decided to set my metadata (regarding .csdl/.ssdl/.msl) programatically with the help from this post. Application now works correctly when deployed to Azure.

application configuration in biztalk

I developed an BizTalk orchestration where I am calling custom library method.
Since my custom library is consuming a web service and writing data into database therefore it reads various info like database connection string , WCF service endpoint address from appconfig. I put my custom library into GAC and deployed the BizTalk application but I am unable to find a place where I can put the appconfig which is used by custom library.
I Googled and found to append the config file in BTSNTSVc.exe placed under :\Program Files (x86)\Microsoft BizTalk Server 2013, however its not the recommended way.
You can save your configuration in BTSNTSvc.exe.config, but that file contains biztalk host configurations.
Take in mind, that if you'll have a syntax error in that config file - you'll have troubles running biztalk engine.
Best solution is to use a cache layer which you will consume by C# class library from your orchestration.
A better option is add to BTSNTSvc.exe.config a redirection to your config file, for example:
<appSettings>
<add key="myConfigFile" value="C:\MyProject\Config\myConfigFile.config" />
</appSettings>
This allows you to modify the configuration of your application without modify each time the BTSNTSvc.exe.config.

Tables not created with Entity Framework on Azure

I am using Entity Framework using Code First and the Fluent API.
When developing on my local machine, the database and tables are created as expected and everything works fine.
When deploying to my Azure web role the database is created, but the tables are never created. If I try a HTTP request touching DbContext, the request times out and finally my web role only returns "Service Unavailable".
The model is large, but not extremely so (43 tables are created on my dev machine).
What can be wrong here? Here is my Azure SQL connection string, where you can see that I set the timeout quite high:
<add name="MyConnectionString" connectionString="Server=tcp:myserveronazure.database.windows.net,1433;Database=MyDatabase;User ID=MyUser#myserveronazure;Password=thepassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=120;PersitSecurityInfo=true;" providerName="System.Data.SqlClient" />
EDIT: After RDP:ing into the Azure VM, I see in the Event Log that w3wp.exe (the IIS worker process, if I'm not mistaken) crashes. The log doesn't list any error message.
Enityframework have three Database Initializer by default CreateDatabaseIfNotExists,DropCreateDatabaseWhenModelChanges and DropCreateDatabaseAlways that are executed when you hit the context for the first time, if you are using migrations (i think is the case) you must use the MigrateDatabaseToLatestVersion initializer to work in the application deployment. Hope it works!

Querying Ef context using LinqPad: Argument Exception

I have added Ef connection in linqpad like following
As you can see connection is successfully added but when i want to query against this connection i get the following exception
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
it seems to be connected with connection string but how it would be solved, i have no idea. The context is defined in asp.net mvc project (no separate project for data access) and connection string is in web.Config file as usual
I had this same problem, I was able to solve it by following the instructions here
1) Find the path that LINQPad uses for its configuration file by excuting this: AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.Dump()
This returned for me the following: C:\Program Files\LINQPad4\LINQPad.config
I was suprised this returned LINQPad.config instead of LINQPad.exe.config
which is what you would typically expect since most .NET applications name the > file the same as the executable.
Take your App.config and copy it to the location above naming the config
file whatever yours returned. In my case it was LINQPad.config
Close LINQPad or the TAB that you have opened to execute your assembly and reopen to get LINQPad to read the configuration file.
http://coding.infoconex.com/post/2012/06/01/Getting-LINQPad-to-read-your-applications-AppConfig-settings.aspx
Recent versions of LINQPad support direct connections to projects with a DbContext or ObjectContext. With that connection type, you can specify your app.config / web.config when setting up the connection.
http://www.kevinlabranche.com/blog/ConnectingLinqPadToEntityFrameworkCodeFirst.aspx