Entity Framework 5 model first - Where is IDisposable gone?
Asked Answered
S

4

14

In Entity Framework 5 model first, there seem to be some breaking changes due to the way the class files are generated (No more code generation, but T4 templates)

2 examples:

  • The generated context file doesn't implement IDisposable anymore
  • There isn't a constructor which takes a connectionstring anymore

Are there more breaking changes? And what is the solution to them?

Seigniorage answered 20/9, 2012 at 7:39 Comment(0)
D
34

The default code generated from a model in Entity Framework 5 now inherits DbContext instead of ObjectContext.

This still implements IDisposable, but if you're getting an error from a line of code similar to this:

using (var mymodel = new MyModelContext()) { ... }

...complaining about not implementing IDisposable, then your problem is most likely that your model is defined in a separate assembly that references EF5 and you have not added an EF5 reference to your project.

As Ladislav Mrnka has already mentioned in his answer, if you want to pass a connection string to the constructor you have to create your own constructor manually to do this.

If you want to switch Entity Framework back to the older style of generated code, which will automatically generate the constructor you're looking for, then follow these steps:

  1. Click on the designer surface of your EDMX file, and look at the properties window. Find a property called "Code Generation Strategy" and set this to "Default" instead of "None". This will tell Visual Studio to start creating the code for your object model in MyModel.Designer.cs in one big file, this time using ObjectContext instead of DbContext.
  2. Delete the following sub files from below your EDMX file: MyModel.Context.tt, MyModel.tt. These are the auto generated files that you don't want anymore. If you don't delete them you'll get class naming conflicts because your objects will be created twice.
Dann answered 24/9, 2012 at 16:9 Comment(3)
Thanks! In case others have missed it, the project that implements the "using (...)" code needs the new EntityFramework.dll, not just the project that holds the .edmx file.Garlicky
@Garlicky could you specify which using reference it needs?Circumferential
Thank you! As well as the reference to EntityFramework I also needed to add a reference to System.Data.Entity and then my "using" error disappeared. :)Saltsman
H
5

The generated context file doesn't implement IDisposable anymore

IDisposable is still implemented by the parent context type. The generated type is still disposable.

There isn't a constructor which takes a connectionstring anymore

It now uses convention to get connection string but you can add your own constructor either to template or to your partial class part of the context.

Are there more breaking changes? And what is the solution to them?

It is whole breaking change because it uses different API - DbContext API instead of ObjectContext API which means different types, different methods, POCO entities etc. If you want to get back to original code generation you have to delete those T4 templates and enable code generation as described in .Designer.cs file but the current recommended way is to use POCOs and DbContext API.

Hartal answered 20/9, 2012 at 8:2 Comment(0)
R
1

I was having the same issue with the using statement needing a type that extended IDisposable... Turns out that I forgot to reference System.Data.Entity in my project... Added the reference and it fixed the problem.

Raceme answered 7/4, 2015 at 1:19 Comment(0)
R
0

Just clean and build the project, don't forget to add the reference to your entity.

Rhodes answered 18/5, 2022 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.