Upgrade EF 4 EDMX to EF 6 - entity-framework

My application is using a database first EDMX in EF 4. I would like to upgrade everything to EF 6. After getting EF 6 with NuGet I had to make a lot of changes to my classes that are using my EF model, because namespaces have been changed in EF 6. Then I realized, that the code generated by my EDMX does also use the wrong namespaces etc. I'm not using a custom T4 so far.
How would I upgrade my existing EDMX to EF 6.
Thank you.

You delete your old .tt files
You open your edmx file in designer mode (so you can see your model)
Right click on a free space
Select Add Code Generation Item
In the dialog select "EF 6.x DbContext Code Generation Item" (something like this)
Save your edmx and all classes will be generated for you, with the new namespaces and so on

In addition to the answers given here by Rand Random and Dean Oliver, let me mention the following MSDN link, describing general steps for upgrading to EF6. Don't underestimate the manual steps required...
The road map is (see details in the link given above):
Preparation: Install the Entity Framework 6 Tools for Visual Studio 2012/13
Install the EF6 NuGet package
Ensure that assembly references to System.Data.Entity.dll are removed (Note: Installing the EF6 NuGet package should automatically remove any references to System.Data.Entity from your project for you).
Swap any EF Designer (EDMX) models to use EF 6.x code generation. Notes:
If you're getting the message "The Entity Data Model Designer is unable to display the file you requested" afterwards, then click on the link modify in the displayed text message "The entity Data Model Designer ... You can modify ...", which will display the tables. Select all with Ctrl+A, then press Del, then right-click and select "Update model from database", and finally save using Ctrl+S. This will update the model to the latest version using the default T4-Template "EF 6.x DbContext Generator".
If you have used ObjectContext in your project, then you should consider downloading the template "EF 6.x EntityObject Generator". Then right-click in the model designer, choose "Add code generation item", then choose a name you haven't used yet. It will generate the right classes, afterwards you have to remove all old ("*.tt") files and related generated class ("*.cs") files.
Update namespaces for any core EF types being used, i.e.
any type in System.Data.* is moved to System.Data.Entity.Core.*
System.Data.EntityState => System.Data.Entity.EntityState
System.Data.Objects.DataClasses.EdmFunctionAttribute => System.Data.Entity.DbFunctionAttribute.
Note: This class has been renamed; a class with the old name still exists and works, but it is now marked as obsolete.
System.Data.Objects.EntityFunctions => System.Data.Entity.DbFunctions.
Note: This class has been renamed; a class with the old name still exists and works, but it is now marked as obsolete.
Spatial classes (e.g. DbGeography, DbGeometry) have moved from System.Data.Spatial => System.Data.Entity.Spatial
N.B.:
More information about available EF templates can be found here at MSDN.
If you're getting an obsolete attribute warning after upgrading to EF6.x, check out this SO article: How to get rid of obsolete attribute warning?

As well as the steps Rand Random suggested. Remember to Install Entity Framework 6 Tools for Visual Studio 2012 if you are using VS2012. download here
This will ensure that EF 6.x DbContext Generator template shows when clicking Step 4: Add Code Generation Item

Related

Entity name conflicts in Visual Studio

I'm new to the Entity Framework. I've created a model-first schema in Visual Studio 2012 with EF5, but I'm having trouble getting the generated code to build. One of my entities ("Test") has the same name as the model (edmx) and the project (csproj) files, so it conflicts with the namespace (Test is a namespace but is used like a type). So I decided to change the namespace by setting a "Custom Tool Namespace" for the .tt files. This worked, but then I found that the "Test" entity's generated .cs file was entirely empty (other entities were generating properly), so I had build errors where other entities reference "Test". Renaming the entity results in a properly generated class, and therefore a building project, but I really want to use the original name.
I will probably end up scratching the project and starting over, ensuring to choose unique names for the project, the model, and the entity. But I'd rather know how to fix this if possible in case I run into something similar when the project is further along and it's not so easy to start over.
you can use use an alias on the Using Directive, e.g. using Project = PC.MyCompany.Project; to differentiate between namespaces. see MSDN

Adding Hand-Built Models to an EDMX

I'm following an MSDN article on applying the Repository Pattern and the Unit Of Work Pattern to Entity Framework, but I'm stuck at the mapping between the custom-made domain models and the as-yet-nonexistant database.
The article has me create two simple POCOs, Employee and TimeCard. It also walks through creating generic repositories and custom implementations therein. (I'm using the custom repositories so I can try to keep EF dependencies in the data access assembly.) However, they sort of glaze over an important step in the mapping. The article says:
With the POCOs in place we can create an Entity Data Model (EDM) in Visual Studio (see figure 1). We will not use the EDM to generate code for our entities. Instead, we want to use the entities we lovingly craft by hand. We will only use the EDM to generate our database schema and provide the metadata EF4 needs to map objects into the database.
The "Figure 1" it references is here:
But that's all it says on the subject. There's an aside on how to generate POCOs from an EDMX. There's lots of information via Google on how to generate POCOs, generate EDMX from a database, etc. But in this walk-through we already have the POCOs and I need to use them in the EDMX which would, in turn, generate the database (I'm assuming, based on other code-first walk-throughs).
I've added an "ADO.NET Entity Data Model" to the project, which is basically a blank canvas. But I'm not seeing how to add my existing POCOs to that canvas. Do I have to re-create them manually on the design surface (which would be a pretty significant duplication problem in a larger domain)? If so, how do they map to the existing ones?
Typically when you use the designer the flow is the opposite - you create the model with the designer (or create/update the model from the database) and then the code is created for you. The created code can be either the EF1 style code with entities derived from EntityObject and attributes etc. which is created with a Single File Generator which is a part of VS (Code Generation Strategy set to "Default") or the code can be created with T4 templates (Code Generation Strategy set to "None") in which case you need to add T4 templates to your project. EF matches POCOs with Entities from the edmx file by convention (names of entities have to be the same, names and types of properties have to match etc.). In the article for some reason they went the opposite way which is weird since it requires that you create all the entities and relationships with the designer manually (since the designer does not know how to create entities from the code) and make sure that the requirements for the conventions (you may not even be aware of some of them) are met. However when you start with code the better approach is to use the EF Code First approach and skip the designer entirely. Code First can create database from your code. It also contains migrations feature which allows evolving your database along with your code. Finally (as you seem to use Visual Studio 2010) you could use EF6 which allows using all the goodness that was previously only available on .NET Framework 4.5 to be used on .NET Framework 4. See here for more details: http://entityframework.codeplex.com/
*the names are going to change in the new version designer that supports EF6 and works with Visual Studio 2012 and Visual Studio 2013
EDIT to address questions from the comment
If you would like to use Code First would use the DbContext API which is a streamlined wrapper of the ObjectContext API. Here is a walkthrough that should help get you started.
You can still use Code First if you have an existing database - the difference is that you will not be able to use migrations. The easiest way to get started with this is to use EF Power Tools. Take a look at this tutorial to see how to do that.
More help here

Entity Data Model Wizard not creating tables in EDMX file

I'm trying the database first approach by creating an ADO.NET Entity Data Model using the wizard with the Adventureworks2012 DB.
Testing DB connection works, and the connection string is added to the App.Config.
I'm selecting all the tables except the ones marked as (dbo) AWBuildVersion, DatabaseLog, and ErrorLog.
When the wizard finishes the .edmx file is blank, and if I view the file in XML view the EntityContainer is empty.
After the model is created it returns this error in the output window:
Unable to generate the model because of the following exception: 'The
table AdventureWorks2012.Production.Document is referenced by a
relationship, but cannot be found.
I'm using VS 2010 & .NET Framework 4.0
It seems that Entity Framework does not know how to deal with data types like hierarchyid set on a table field. I removed the Production.Document table for the list of entities to include solving my problem.
Note also that this reference below was for Adventureworks 2008R2 with EF version 1.0 from Code Plex SQL Sever, and I am using Adventureworks 2012 from the same CodePlex site using EF version 4.4.
Reference: http://msftdbprodsamples.codeplex.com/wikipage?title=AW2008Details
Note: EF 1.0 Compatibility Issues
The Entity Framework team would like us to let you know that AdventureWorks2008 is a little bit ahead of the curve in terms of the Katmai features it uses. Some datatypes in AdventureWorks2008 (such as hierarchyid and geometry) are not supported in the entity framework. The workaround is to exclude tables like Production.Document from your model if possible since there is currently no support for the hierarchyid datatype in Entity Framework 1.0. Unfortunately the Entity Framework tooling which updates your model from the database will pull in tables like Production.Document even if they were specifically excluded when the model was created, so use of that feature on AdventureWorks2008R2 is not supported at this time. We look forward to a follow-on release of Entity Framework which has full SQL Server 2008 type support.
Last edited May 25, 2010 at 2:22 PM by bonniefe, version 17
There is a way to get around this IF you're trying to learn from this example and not doing anything meaningful. I deleted the foreign keys to the offending table and removed it and was able to succesfully get
Uncheck [Allow Nulls] Check-boxes (in the table design) for all the foreign keys of the not created Tables (Tables not converted to the model).
Then you can update your model to retrieve those tables by doing the following steps:-
Step 1 - Right click some where in your .EDMX file's design (i.e. Model1.EDMX [Diagram1].
Step 2 - then from the Context Menu select / Update Model from Database....
Step 3 - then select "Add"
Step 4 - then expend "Tables" Check-Boxes and select your desired tables (tables not created first time).
Step 5 - then click Finish button.
Step 6 - Save the the solution and hope everything will be Ok.
Note: I'm using Visual Studio 2013.
Good luck.

Entity Framework 5 - How to generate POCO classes from existing database

I am using VS 2012 and EF 5. I have an existing database that I want to create POCO classes from the existing database. I followed the steps to add an ADO.NET Entity Data Model to my project. I went through the wizard to use an existing database. It then created the edmx and tt files with the designer open. However, I want to create the POCO objects and use them. The Microsoft site states that the POCO Entity Framework Generator is obsolete and I should use the DBContext Generator. I can't figure out steps I use to generate the POCO classes. I only see the edmx designer. I really don't even want an edmx file but instead just POCO classes. How can I get POCO classes to be created from an existing database using EF 5 and VS 2012?
Use EF 5.x DbContext Fluent Generator
You can add it from online templates:
Generate edmx from existing database
Select Add New Item
Search online templates for POCO
Add EF 5.x DbContext Fluent Generator
It will add three T4 templates to your project:
XXX.Context.tt - context inherited from DbContext
XXX.Entities.tt - POCO entities
XXX.Mappings.tt - fluent mappings for each entity
BUT you need to setup path to your edmx data model manually. Each of these templates have line string inputFile = #"$edmxInputFile$";. You need to provide name of your edmx file here:
string inputFile = #"Northwind.edmx";
The process to do this is pretty streamlined now, it seems. The steps from the accepted answer are now easy to do from the EDMX designer itself. Basically,
Generate the model (edmx) from an existing database by adding ADO.NET Entity Data Model to the project (see here for more details),
and then
Open the .edmx file in the Entity Designer.
Right-click an empty area on the Entity Designer surface and point to Add Code Generation Item.
In the Add New Item dialog, select Online Templates and type DBContext in the Search Online Templates text box.
Select the appropriate version for your template (5.0, if you want to target the Entity Framework 5.0).
Click OK.
This will do all the work, apparently. The quoted instructions here refer to Online Templates as installing EF 5.x DbContext Fluent Generator is required. If you have it already installed, there is no need to search for it in the Online Templates but in the Installed Templates.
For more info you can check this page, section "To use the DbContext Generator Template to Generate Object Layer Code".

Issue with AssociationSet in EF when changing target from .NET 4.0 to 3.5

I developed a Repository component targeting the .NET 4 Framework that uses EF. In my EF I use tables already existing in the Database.
In the Database I have 4 tables representing entities and 3 association tables (the relationships between the entities are all many-to-many)
However these tables at Database level do not have any relation between each other. Since I do not have rights to modify the Database, I added the relations directly in the EF. I named them, I specified the navigation properties and then I successfully compiled and tested the project.
Now I have to reference this Repository project from a Web application using the .NET 3.5. Therefore also the Repository project must reference the .NET 3.5.
I correctly copied the connection string from the Repository project to the web.config, I compiled the project and...it gives me a compile error saying that there is
"no mapping specified between EntitySet/AssociationSet"
and then lists the relationships between tables/entities.
I guess this error is given because there are no associations at Database level. How can I solve this issue without having to rewrite all my code?
You cannot use an EF 4 EDMX in EF 3.5 period. You can, however, use an EF 3.5 EDMX in EF 4, but the EF 4 designer won't understand it.
One possible fix is to make your EF 4 project an out of process server.