Share edmx Betwen .NET projects and C# Class Library - entity-framework

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.

Related

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.

Code First Migration and creating a mdf in a class library

I have a Solution with three projects. One are the models, another is the repository and context configuration and the last one is a Web application.
The things is that when i configure the Entity Framework in the Repository Project and add the migrations, and i try to execute update-migration nuget console this throw me and error.
I need the database in a mdf file so the app.config look this way:
The error when i try to update-migration is:
The physical file name "/ExampleDB.mdf" may be incorrect
By the error i understand that |DataDirectory| is not working and i can't set it with
AppDomain.CurrentDomain.SetData("DataDirectory", Directory.GetCurrentDirectory()); because this project is a class library.
Thanks a lot

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).

Publish project with embedded DB connect with entity framework

I have a SQL Server CE 3.5 DB which is set to "Embedded" vis a vis Properties. The database has an associated .edmx file and entity-framework code works when I run it from VS 2010.
The app.config string is the following:
<add name="MapProjectDB2Entities1" connectionString="metadata=res://*/MapProjectDB.csdl|res://*/MapProjectDB.ssdl|res://*/MapProjectDB.msl;provider=System.Data.SqlServerCe.3.5;provider connection string="Data Source=MapProjectDB2.sdf"" providerName="System.Data.EntityClient" />
But when I Publish my project and run it from the one-click deploy folder, the exception is "The Underlying Data Provider Failed to Open".
So I need to somehow connect to my embedded database. Thanks in advance the google hits are a bit spotty for this one.
I should mention that the DB is member of a library (DLL) project, which is loaded from another project.
I needed to add the DB to my Exe project. From there I needed to change the config string to be the following:
The difference is only the |DataDirectory|\ part
<add name="MapProjectDB2Entities1" connectionString="metadata=res://*/MapProjectDB.csdl|res://*/MapProjectDB.ssdl|res://*/MapProjectDB.msl;provider=System.Data.SqlServerCe.3.5;provider connection string="Data Source=|DataDirectory|\MapProjectDB2.sdf"" providerName="System.Data.EntityClient" />

How to have multiple projects accessing same Entity Framework project?

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?