How to have multiple projects accessing same Entity Framework project? - entity-framework

Ok, this question has been asked many times. Just googling with the error message:
The specified named connection is either not found in the configuration,
not intended to be used with the EntityClient provider, or not valid.
I have tried all suggestions on forums, help sites et blogs... nothing works!
Situation recap
I'm splitting solution into three different projects: one for my model with EF4, one of WinForm and one ASP.NET MVC web application. I have a problem with connections string and configuration.
Is there a updated solution using VS2010/EF4/.NET Framework 4.0?

Not sure, but you may have to move the Entity Framework configuration sections in your EF4 project's app.config file to both your WinForm app.config and ASP.NET MVC web.config files.

Really, the answer is copying the <connectionStrings> section in the other project's config files like mentionned above.
<connectionStrings>
<add name="DataEntities" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=xxx;Initial Catalog=xxx;User Id=xxx;Password=xxx;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
So, it's the same answer as I saw before on many sites. In fact, it was working from the beginning, but I had another error ("Configuration system failed to initialize"), so I thought it was a wrong value of the connection string in the app.config. In fact, the app.config was valid, but just moving the section on bottom solved this... Bizarre?

Related

Reverse Poco only Generating initial comment block

I'm trying to generate models from an existing database using the Reverse POCO generator. I've not made any changes to the default template file and I've called my connection string "MyDbContext".
<connectionStrings>
<add name="MyDbContext" providerName="System.Data.SqlClient" connectionString="Data Source=xx.xxx.xx.xxx\SQLEXPRESS;Initial Catalog=ZooQ;Persist Security Info=True;User ID=ZooQDev;Password=******;"/>
</connectionStrings>
I've checked the credentials are ok by testing the connection in visual studio, so I know they are right. Only difference to the usual way I do this is that now I'm accessing a VM server on Azure, but I wouldn't think that would be the problem.
I've tried uninstalling and reinstalling the generator, uninstalling and reinstalling Entity Framework, deleting and re-adding the template file and closing and re-opening Visual Studio.

EF works but I can't find the database (but only in the test environment)

My three environments are all acting differently.
EF came with my chosen MVC website project template and I haven't messed with it. I do have a mdf file in my App_Data folder. The location and filename were determine by the nuget installer I assume. Identity framework uses it. To give you some context.
My web.config ef connection string is:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-AACOMvc-20160730112925.mdf;Initial Catalog=aspnet-AACOMvc-20160730112925;Integrated Security=True" providerName="System.Data.SqlClient" />
This is the connection string provided by the template / installer whatever.
Local Machine
It works. If I mess with the connection string or the mdf filename, it breaks. So I know it's hitting that file.
Test Environment
It works. Funny because I don't publich the App_Data folder! I have searched all over that box looking for the mdf (it created?) but there is not one. If I comment out the connection string in the web.config, it still works! It's being "smart" and "sneaky" and I don't like it! :)
Production Environment
It doesn't work. Even if I copy over the App_Data folder, it still will fail with "file not found" error.
This question is focusing on the test environment. If I can find out how it's working, I think I will be closer to getting it to working in production.
I recall I fixed this by changing the EF connection string to a sql server instance instead of a sql server file. EF didn't even create any tables in the database b/c I don't have any entities defined.

Publish Web dialog doesn't detect my entity framework 5 context as Code First

I've recently upgraded a WFC project that uses Entity Framework from v4.3.1 to 5.0.
I'm running Coded migrations only (no automatic migrations).
Previously, I was using the Publish Profiles to deploy this solution and apply the migrations. Since upgrading the project to EF5, the migrations portion no longer works and the publish dialog doesn't detect the context as having code-first migrations.
Specifically, the .pubxmlfile changed from detecting my context as <Object Type="DbCodeFirst">to <Object type="DbDacFx"> which is incorrect for my context.
As a workaround, I've manually added the <entityFramework> database initializer configuration to my web.config transforms, but I'd like to understand why the publish profiles aren't working. That was a much nicer solution.
It once happened on me when merging another developer's commit and triggered Visual Studio project reload. That's how it caused the "DbCodeFirst" to "DbDacFx" change.
If I restart Visual Studio then everything goes back to what it should be.
Just another thought.
You probably missed adding the reference to EntityFramework into your project. By just adding the reference you should be able to control whether or not the DbCodeFirst option is available or not.
As this post suggests, try using the fully-qualified name of your DbContext as the name of the connection string. Instead of:
Web.config
<connectionStrings>
<add name="MyContext" .../>
</connectionStrings>
Use:
Web.config
<connectionStrings>
<add name="MyNamespace.AnotherNamespace.MyContext" .../>
</connectionStrings>
In my case, in order to use my existing publish profiles (.pubxml), I also had to manually edit the <ObjectGroup Name="..." ...>. Probably recreating the publish profiles would work too.
I had the same issue but not in the same context.
I had been using Code First Migrations with existing ASP.NET MVC 5.2.3 application using EF 6.1.3 for a month without issues. At one point in time I added support for Windows Azure Storage but I made some mistakes:
I added a new project. Unfortunately I chose "Console Application" instead of "Class Library". I tried to fix it by changing it back to "Class Library" in "Project Settings"
I used Nuget to Install-Package WindowsAzure.Storage but I installed it on the MVC project and not on the class library. I tried to fix it by doing Uninstall-Package on the MVC project and installing it to the correct project
I called the class in the class library "WorkOrderStorage"
I added the connectionString to <connectionStrings> element in web.config and a transformation in web.release.config
I guess my project was now in an inconsistent state. I noticed that it would forget about Code First Migrations (I monitored the changes to the .pubxml file):
when I put a reference between the MVC project and the library
containing the WorkOrderStorage class
when I created an empty 'WorkOrderStorage' class in one of the existing libraries
In the end I fixed it by recreating this library correctly from scratch as a class library (because of observation 1). I also named the class WorkOrderRepository (because of observation 2).

Entity Framework Code First Data Migrations not working with VS2012 Web Deploy

I have created an MVC 3.0 application using Visual Studio 2012, .NET 4.5 and Entity Framework 5.0.
Using Code First Data Migrations, I am able to correctly propagate model changes to my local test database, but I can't figure out how to get this to work when deploying to my staging and production servers using Web Deploy.
I have read the following article ...
http://msdn.microsoft.com/en-us/library/dd394698(v=vs.110)#dbdacfx
... which explains what's supposed to happen, but it's not working for me, as Web Deploy seems unable to detect that I am using Entity Framework. The tutorial shows a checkbox to enable execution of Code First Migrations ...
... but my dialog shows the only Update Database checkbox for each database.
I have read that, in order for Visual Studio to detect the use of an Entity Framework context, the Web.config must include an element that defines it. Here's mine:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<contexts>
<context type="MyContext, MyAssembly">
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MyContext, MyAssembly], [MyConfig, MyAssembly]], EntityFramework">
<parameters>
<parameter value="MyConnectionStringName"/>
</parameters>
</databaseInitializer>
</context>
</contexts>
</entityFramework>
Any suggestions would be greatly appreciated.
Thanks,
Tim
I have discovered the solution to this problem through experimentation.
In fact, my MVC application was created by VS2012 by converting from a VS2010 solution. Since the conversion process did not report any issues, I assumed that it had correctly converted everything, including the publishing profiles.
However, I discovered that the problem was in the conversion of these profiles and, unless I manually edit their XML files, there is apparently no way to get old imported profiles to participate in Code First Migrations.
Simply creating new publishing profiles in the converted solution results in the expected behavior.
Try this:
Enable-Migrations -Force
Ensure the below is set to true
AutomaticMigrationsEnabled = true;
Republish - worked for me

Share edmx Betwen .NET projects and C# Class Library

I had two seperated projects that used the same database, similar methods, etc.
So i've created a new solution and added those two projects.
I've created an C# Class Library project and i've writed there all the methods from the other two.
I've also created an edmx file for DAL layer. I've added the reference of the C# Class Library projects to others two but now i'm having an problem.
When i try to connect to database i get the error message: "The connection specified wasn't founded in aplication, doesn't destinate to be used with the EntityClient or isnt't valid" (i've translated this from Portuguese, so it's 100% correct).
In App.Config of C# Class Library, the connection string is:
<add name="RIAEntities" connectionString="metadata=res://*/RIA.csdl|res://*/RIA.ssdl|res://*/RIA.msl;provider=System.Data.SqlClient;provider connection string="Data Source=GERAL-PC\SQLEXPRESS;Initial Catalog=RIA;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
Do i have to add a connection too in web.config of the other 2 projects that will use edmx from C# Class Library?
If yes, can someone show me an example?
Thanks
I've resolved it by just copying the connection string of App.Config (C# Class Library) to other 2 projects.