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

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.

Related

Can you manually add tables and references to SPs/Views/Functions in EF Core Code First to an existing DB?

In EF 6 my work flow was to make all DB changes directly in SQL Server and then manually update/add EF classes to match what's the in the database. What I want to avoid is driving the DB design from code or scaffolding from the DB into EF.
I just want to manually manage everything once the DBContext has been generated.
Is this still possible in EF Core?
I just want to manually manage everything once the DBContext has been generated. Is this still possible in EF Core?
Absolutely. Same as in EF 6 Code First, just create the classes and map them to your database objects.

EF Context missing a collection

I'm trying to generate an entity from my SQL database using the ADO.NET Entity Data Model using the ADO.NET DbContext Generator. When I generate my edmx from the database I can see it in the model. I right click on my tt file (which is in a separate project) and run the custom tool. The entity appears. It's called CustomerContact. However, my dbcontext does not have a CustomerContacts collection. What is going on here?
I figured it out. The problem was the different versions of Entity Framework. I updated both projects to the latest version of EF and it works now

Entity Framework 6 with Sql Server Compact 3.5 SP2?

I have found lots of information about using Entity Framework 6 with Sql CE 4 and on using Entity Framework 4 with both Sql CE 4 and 3.5, but using Entity Framework 6 with Sql CE 3.5 is a different matter. As far as I can tell, CE 3.5 ships with a provider for Entity Framework 4 in System.Data.SqlServerCe.Entity.dll. This provider uses an older version of the base classes and if I try to use it, I get the following exception:
System.InvalidOperationException: The 'Instance' member of the Entity Framework provider type 'System.Data.SqlServerCe.SqlCeProviderServices, System.Data.SqlServerCe.Entity, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' did not return an object that inherits from 'System.Data.Entity.Core.Common.DbProviderServices'. Entity Framework providers must inherit from this class and the 'Instance' member must return the singleton instance of the provider. This may be because the provider does not support Entity Framework 6 or later; see http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
This makes sense and points me to the realization that what I need is a provider for Entity Framework 6 for Sql Server CE 3.5 SP2. I can't seem to find such a thing though. The nuget package EntityFramework.SqlServerCompact seems to only reference Sql Compact 4 in all its versions. Does such a provider already exist? If so, where can it be found? I seem to be having difficulty locating it. If not, would it be realistic to think of implementing it by using the code in the one for CE 4 with a reference to 3.5, or is the difference between CE 3.5 and 4 too different for that to be straightforward? Is there another workaround?
I need to use 3.5 because of a need for using merge replication, which is not supported in 4, and would like to use Entity Framework. If there's not a straightforward answer to this, I'll probably use something other than Entity Framework.
OK, this is the second request I hear for this, let us do it. I will fork the code, and make a Nuget package for 3.5 - how about "EntityFramework.SqlServerCompact.Legacy" ? The "only" code change required is the fact, that 3.5 does not support ORDER BY..FETCH..OFFSET syntax (ie paging) called via Take and Skip. Would you be interested in this?
UPDATE: Package now available on NuGet http://www.nuget.org/packages/EntityFramework.SqlServerCompact.Legacy

Upgrade EF 4 EDMX to EF 6

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

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.