none
How do I convert an EF 4.0 ObjectContext-based model to an EF 4.1 DbContext-based one when the application and the model aren't NEW? RRS feed

  • General discussion

  • WHAT I HAVE:

    Visual Basic 2010, .NET 4.0, WinForms, Entity Framework 4.1 (I think!)

    MY PROBLEM:

    Some time ago, I created a VB2010/EF 4.0 project--using model-first and generating standard ObjectContext-inherited classes--and then encountered a problem--the run-time entity.Remove method doesn't work for entities who relationship is many-to-many. I looked up a simple one-statement fix (using Entry/Collection/Load methods)--but it requires using EF 4.1 and generating DbContext-inherited classes.

    So, I installed EF 4.1 and have tried to convert the model to from OC to DC--but I'm having problems!

    I read the instructions at "EF 4.1 Model & Database First Walkthrough"

    (http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx)

    and I've had several problems. (NOTE: I'm working with an existing project and model, not a brand new one.)

    1. After going through "Generate Database from Model" and right-clicking on the generated script and selecting Execute SQL, I can't seem to find a server to connect to, even though I obviously DO have one.
    2. When I invoke "Add Code Generation Tool" and click on ADO.NET DbContext Generator, I get several messages on how loading the template could "potentially harm" my computer. Also, if I don't previously NOT ONLY set the model's "Code Generation Strategy" to default but also Save the project first, no script or DbContext-inherited classes are generated.
    3. When the classes are created, a conflict is created with the existing ObjectContext-inherited classes. I've even tried to creating a second model, copying the entities from the original model to the new one (a little tricky with respect to complex properties!), then deleting the original (simply by deleting its .edmx file and corresponding designer file?), and applying the above steps to the new one. 

    The web page I mentioned above uses an example of a newly-created project and model. What do I do to make the conversion from OC to DC for an existing project and model? (I've gone to far to start over from scratch!) I've encountered other errors in the process, but they seem to be the kind that can be fixed by editing member and type names.


    Robert Gustafson

    PS.

     I've encountered other errors in the process, but they seem to be the kind that can be fixed be editing member and type names throughout my main code.

    PPS.

    If the solution requires upgrading NuGet, that always fails on me. (And it won't let me uninstall-and-reinstall it either.)

    Wednesday, April 27, 2016 2:28 AM

All replies

  •  (I've gone to far to start over from scratch!)

    Well, it may come to that, and you just may have to go to VS2012, 13 or 15 that has depreciated the ObjectContext and the preferred context is DBContext in those versions of EF and the .NET Framework.

    Friday, April 29, 2016 6:30 PM
  • If I upgrade to a later version of VS (upgrading to VS 2015 cost $499!), how difficult is it convert my model from OC to DC? Do I have to copy my model and delete the original (I've tried that, and I'm not sure how one "removes" an old model from a project)? I need advice! Anybody who can show me how to convert my model--with or without spending $499--I'm listening. (BTW, when I said I can't "start over from scratch", I was referring to the program as a whole. I can rebuild the model in 20 minutes, but I can't rewrite 1,000+ lines of my own code [although I could edit that code to account for OC-to-DC related changes].)

    Robert Gustafson




    Sunday, May 1, 2016 2:08 AM
  •  I was referring to the program as a whole. I can rebuild the model in 20 minutes, but I can't rewrite 1,000+ lines of my own code [although I could edit that code to account for OC-to-DC related changes].)

    As long as the objects on the virtual model and the properties of the objects  remains the same, then I don't see why you can't build the model from scratch to use DBcontext, and your lines of code basically stay as is except you are changing limited code dealing with Objectcontext to DBcontext.

    I mean it is what it is for you. 

      

    Sunday, May 1, 2016 7:15 PM
  • That's what I thought. Problem is, if I try to create a DBContext from my existing model, the ObjectContext-inherited class persists. If I remove the original model (and I'm not sure how to properly do this) and rebuild it using DbContext, once again I still have an ObjectContext-inherited class in addition to the DBContext-based classes. What am I doing wrong?

    Robert Gustafson


    Monday, May 2, 2016 3:09 AM
  • That's what I thought. Problem is, if I try to create a DBContext from my existing model, the ObjectContext-inherited class persists. If I remove the original model (and I'm not sure how to properly do this) and rebuild it using DbContext, once again I still have an ObjectContext-inherited class in addition to the DBContext-based classes. What am I doing wrong?

    Robert Gustafson



    There is no existing model if you build the model from scratch. It means delete the existing model out of the solution and create a new one from scratch using DBcontext. 
    Monday, May 2, 2016 7:27 PM
  • How do I properly delete the model from the solution? And when creating a new one, the approach is Entity  Data Model>build model>Generate Database from Model>DbContext, right? And is it acceptable to rebuild the model by copying the objects (Ctrl+A, Ctrl+C in the model window) before deleting the model, and pasting them after creating a new one (Ctrl+V in the model window)--or does that explain why I keep getting OC-based classes recreated in addition to DC-based classes? (Also, why do I get template warnings about possible damage to my PC every time I select DbContext?)

    Robert Gustafson






    Monday, May 2, 2016 11:18 PM
  • And when creating a new one, the approach is Entity  Data Model>build model>Generate Database from Model>DbContext, right?

    I can't tell you I use DB First, the only one I use. It is the simplest to use for simple people like me.

    Monday, May 2, 2016 11:52 PM