Is there an ObjectContext generator for Entity Framework 5.0? - entity-framework

I am looking or an ObjectContext generator for Entity Framework 5.0, not a DbContext generator. WCF Data Services requires an ObjectContext and won't work with a DbContext. If I want to use EF5 with a DataService I must provide an ObjectContext. The ObjectContext generators in Visual Studio 2010, and online are for "ADO" and for "EF 4.x". The generators for "EF 5.x" all product DbContext objects.

Right click the white space in the designer and select Properties, then change the Code Generation Strategy to "Default". You'll have to delete the code-first TT files, too.

Although too late - you will have to first delete everything under your edmx file (all the tt templates). After that you will have to download the code generator that creates ObjectContext. To do so right click in your edmx and select Add Code Generation Item. After that select Online (on the left) and select EF 5.x EntityObject Generator for C# - this is the generator that creates ObjectContext, you will see it in the description (this is also valid for EF 4.x, just find the same generator, but with 4.x in the name). Install it and you will have your model under the edmx that inherits ObjectContext.

using System.Data.Entity.Infrastructure;
ObjectContext context = ((IObjectContextAdapter)DbContextObject).ObjectContext;

You would probably want to update the version of WCF Data Services to 5.x. Then it will work with DBContext.

Related

Entity Framework - Conext.tt and Model.tt files missing

I'm new to Entity Framework and adding a 'ADO.NET Entity Data Model' item to my Azure Function for a Database table and only two out of the four files I was expecting were generated automatically. Solution explorer screenshot below of what was created vs expectations. What I am I missing so the Entity Class and Context files will generate automatically?
I have Visual Studio version 16.11.5 installed and I am using Entity Framework version 6.4.4
What was created
enter image description here
What I was expecting
enter image description here
Figured this out. The problem I had was I was creating the Entity Model within my Azure Function. I created a separate .NET Framework Class Library and all the model files created as expected.

trouble scaffolding entity framework based controller in ASP.Net Core 2.0

I'm using Visual Studio 2017, v15.3.1 and am trying to follow a tutorial about creating some WebAPI endpoints with Entity Framework, as outlined here
In my case I'm using the good old Northwind SQL database instead of the author's blogging example, which I don't think should be an issue?
At any rate, when I get to the bit where I'm supposed to create a controller, I right-click the controllers folder in my solution, select 'API Controller with actions, using Entity Framework' and choose my EF model class (Orders, Employees, it doesn't seem to matter) and the Data Context which I generated successfully earlier in the tutorial.
When I select Add there's an attempt to scaffold, but I'm then told in a popup: There was an error running the selected code generator: 'No parameterless constructor defined for this object'.
I've looked at this error message online and others have had similar problems, but nothing they tried quite matches my scenario, and their solutions haven't fixed this issue.
My generated EF model classes do have parameterless constructors defined, so I'm not sure what the error is referring to?

ObjectContext instead of DbContext

I'm completely new to EntityFramewok but I have to write a project with my friend with deadline next week. He created Data Access Layer and shared code with me. After using ADO.NET Entity Data Model I found errors in his classes. It appeared that he used Add() method which I don't have. After quick search I found that his generated templated for Context inherits after DbContext and mine after ObjectContext.
Why does it happen so?
We are using the same version of EntityFramework.
As per your comment - This is because you are using VS2010 and he is using VS2012. In VS2012 the default code generation changed from ObjectContext to DbContext. You could change it back to ObjectContext by deleting T4 templates and changing the "Code Generation Strategy" setting from "None" to "Default".
However there is more to the story - out-of-the box VS2010 supported only v2 EDMX while VS2012 supported V3 EDMX, if you create a model on VS2012 you won't be able to open it in VS2010. Similarly I am not sure if you can target .NET Framework 4.5 in VS2010 but it is the default in VS2012.
Ideally you should use the same environment because there is simply to many differences between what you can do in VS2010 and VS2012. If you cannot unify your environments I would recommend targeting the same .NET Framework (would have to probably be .NET Framework 4 unless VS2010 can target .NET Framework 4.5) using EF6 (latest and greatest, bin deployable, does not depend on System.Data.Entity.dll - important given the .NET Framework 4.5 is an in-place update and you never know whether your app targeting .NET Framework 4 is actually running on .NET Framework 4 or .NET Framework 4.5) and using CodeFirst approach to avoid v2 vs. v3 Edmx problems.
DbContext simply acts like a wrapper around ObjectContext. What you could do is create a helper class with a method that exposes your ObjectContext from DbContext:
using System.Data.Objects;
public static class DbContextExtentions
{
/// <summary>
/// Exposes the ObjectContext from DbContext
/// </summary>
public static ObjectContext ToObjectContext(this DbContext dbContext)
{
return (dbContext as IObjectContextAdapter).ObjectContext;
}
}
Usage:
var myObjectContext = DbContextExtentions.ToObjectContext(myFriendsDbContext);

How to disable code-first feature in EF (MVC4 Visual Studio 2012)

How to disable code-first feature in EF (Visual Studio 2012)
I am using Visual Studio 2012, MVC4 (Internet application template).
I want to use EF, but not with its code-first feature. I would want the application to error out, rather than create or modify my database based on my code. (i just can not live with this feeling of my database being changed behind the scenes ... i want the application to use the exact db i have created ... and if there is any thing that has to be changed, i'll do it my self)
is this possible with the new Ef (VS2012)?
i have seen many people asking this, but so far i am unable to find the answer.
You can use Code First and ensure that your database never gets updated or overwritten when you change your model by setting the database initializer to null:
Database.SetInitializer<MyDbContext>(null);
It's a static method of the Database class and should be called at the beginning of your application, for example in global.asax or in a static constructor of your context class. Doing this you have to change model class and database schema manually so that they match.
You can also use the Reverse Engineer feature to create a Code First model from an existing database. It is explained here: http://msdn.microsoft.com/en-us/data/jj200620
Or if you don't want to use Code First at all and work with a model designer you can use the Database First approach, explained here: http://msdn.microsoft.com/en-us/data/jj206878
An overview about all the possible options is here: http://msdn.microsoft.com/en-us/data/ee712907.aspx

EF4 C# Poco Templates

I have installed EF4CSharpPocoTemplates.
Still I am not able to see POCO entity generator in my
Addnewitem->code.
It will show only if you use .NET 4.0