The latest version of C1DataSource (part of Studio for WinForms) now supports Entity Framework version 6.x. In my previous two posts I focused on discussing the changes in C1DataSource and using C1DataSource in a new project. In this post I will show how to convert an existing project to use the new EF6.x-compatible C1DataSource. For many of you the real test of the new control will be its ability to work with existing models that you have created and which may well be in use in applications that are already in production. This section will hopefully guide you through the steps you need to take. It follows those steps that I took with one of my own models when testing the new control. You may choose to perform some of the initial steps in a different order but that should not make any difference to the eventual outcome. The model that I will be using as an illustration was built against a relatively large relational database comprised of nearly two hundred tables. It was originally built using EF4.0 and the .Net Framework 4.0 in Visual Studio 2010, in its own separate project allowing me to keep as much code relating to data manipulation as I could in one place. The model was rebuilt in Visual Studio 2012 against the .Net Framework 4.5 and EF 5.0 because I had wanted to make use of Enum support and some of the improvements that had been made to the way that stored procedures were handled (the database upon which this is based has upwards of three hundred stored procedures and counting). I elected to do the conversion in Visual Studio 2013. The project would remain targeted against .Net Framework 4.5 but would obviously use EF 6.x as opposed to EF 5.0. This particular copy of Visual Studio has the Productivity PowerTools installed (http://visualstudiogallery.msdn.microsoft.com/dbcb8670-889e-4a54-a226-a48a15e4cace). They are NOT a pre requisite to this process but may help to explain some of the things that you will see in illustrations of the Solution Explorer that follow. Begin by making a suitable backup of your project containing the DataModel that you wish to convert. In truth I would make more than one, you can always delete them at a later date and it’s certainly easier than having to recreate a complex model from scratch. Below is an illustration of the original model as rebuilt in Visual Studio 2012 showing that it had been built using the legacy Object Context as was still possible in that edition. I began the transformation by removing all references to the Entity Framework:
- Delete the EntityFramework reference.
- Delete the packages file.
- Delete the Packages Config file.
Now add a reference to Entity Framework 6. To do this:
- Right click on the name of the project and from the context menu that appears select "Manage NuGet Packages."
- In the dialog that appears select EntityFramework (making sure that it is the 6.x version), and click Install.
Once it has installed you’ll see additional references in the Solution Explorer. The red squiggles you also see are the Productivity PowerTools informing me that the project now contains errors. For the time being ignore these, right click on the design surface of the diagram and select "Add Code Generation Item…" from the context menu. Select the ComponentOne Template from the list of Code Generation items (item no 1 in the illustration below), and then click Add. The model will then be rebuilt using the ComponentOne DbContext Template. Dependent upon the size of your model this may take a while. When completed though you will see two new .tt files added to the model and if you had any pieces of code that were specific to the old ObjectContext against which your model had previously been built they will now show up as errors. In the case of the model I’ve been using we can see one file (the partial class for the entity model itself) that I had created to hold some specific code to be used to automate the process of amending the ModifiedDate column that exists in many of my tables in the database. And here is the offending Code itself; We’ll come back to this particular error shortly but for now let’s turn our attention to the main project (or it could well be projects) that are referencing your newly converted Data Model. Looking at the Solution Explorer I can see that there are multiple errors across various files.
A closer look at these shows that the first thing that has been picked up is that the project is still referencing the old C1DataSource dlls.
We’ll begin by removing those. Delete items 1 (C1.Data.Entity.dll), 2 (C1.LiveLinq.dll) and 3 (C1.Win.Data.Entity.dll).
Add the new versions of these supplied with the new version of the C1DataSource and also add to your project the NuGet Entity Framework package. This should remove the primary errors that we were seeing but now we’ll see some new ones.
These errors are being caused specifically by the addition of the new references. If you read the section on using the new C1DataSource in a new project you’ll remember that we made a very specific reference to the fact that the property name that references the entity context that it will use has been changed from "ObjectContextType" to "ContextType" (allowing the use of either Object or DbContext). To correct this all we need to do is change the property name listed in the various designer files. The quickest way to do this is to do a Quick Replace across the project.
Go to the Edit Menu and from it select Quick Replace. Fill out the dialog that will appear as per the illustration below.
Now click Replace All. You may see the following warning;
You can safely click yes. This will solve those errors.
Any errors that are left will most likely be caused by different code constructs that are either ObjectContext specific or by the fact that you need to have Using / Imports statements in your files that reference the new Entity Framework dll’s.
Intellisense comes to our aid when solving these errors.
In this example selecting the first option ‘Import System.Data.Entity.Core’ will solve the problem. You’ll need to run through all the rest of the files in your project that indicate errors but in all cases you should find that intellisense will provide the correct solution. That should correct the problems in the main project and indeed any others that reference the Data Model. There is still the one remaining problem in the Data Model, which we left earlier, that we need to correct now. Many Data Tables have a ModifiedDate column. These are used for a variety of reasons and typically they will store the current date/time that a record is created or modified. Julie Lerman (in her seminal Programming Entity Framework) illustrated an excellent way to do this with the ObjectContext (it is in fact demonstrated in the error that we showed earlier). Now that we are using DbContext this doesn’t work so it will need to be changed.
Interestingly DbContext provides some hooks into the context that actually make this task easier. We can in fact override the SaveChanges() method. In C# it would look like this;
For those of us who prefer to use VB.Net it transpires that this is a surprisingly difficult piece of code to translate, as most freely available code converters will throw an error when trying to convert it. The reason is that this happens to be a Linq statement and the implementation of Linq in VB is very different to its implementation in C#. In VB.Net it would look like this.
Modify this file accordingly if you happen to have something similar. NB: if you have built a model using DbContext then for this to work when you call the SaveChanges method of the C1DataSource you will need to call it like this: C1DataSource1.ClientCache.DbContext.SaveChanges For models built against the ObjectContext you can continue to call C1DataSource1.ClientCache.SaveChanges
The final thing that you may need to change is the Licences file in your project(s). Any references to the old version numbers of the C1DataSource will need to be erased. Just delete all the extraneous information back to the basic entry for the control as shown below.
Now all that is left is to rebuild your entire solution and run it. You should find that everything continues to work exactly as it did before. It took just under ten minutes for me to convert this solution and in truth I would not expect it to take much longer for one that was substantially more complex.
Resources for working with the DbContext
It wouldn’t really be appropriate to go through a long list of differences between the Object and DB Context’s here or indeed how you should work with the DbContext. There are however a large number of resources available to you both in print and online where you will find additional information. I should point out at this stage that for those of you who Code in VB.Net (myself included) almost all of these feature code examples in C#. This can be particularly frustrating, especially when those example go on to utilize Linq simply because most code converters have such a tough job converting them. As a result it can be a painful experience getting them to work but it’s worth the effort to be able to leverage the full power of the Entity Framework. No article on the resources for Entity Framework would be complete without mention of Julie Lerman. In addition to her excellent Programming Entity Framework, she and Rowan Miller (the head of the EF team at Microsoft) collaborated on two books; "Programming Entity Framework: Code First" and "Programing Entity Framework: DbContext." Both concentrate on explaining the DbContext API as it was in EF V4.1. Visual Studio Toolbox on Channel9 has some excellent shows on Entity Framework featuring both Rowan Miller and Julie Lerman; http://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Visual-Studio-Toolbox-Entity-Framework-Part-1 http://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Visual-Studio-Toolbox-Entity-Framework-Part-2 http://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Entity-Framework-5-and-6 http://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Entity-Framework-Tips-and-Tricks Julie has produced a series of courses on Pluralsight: http://pluralsight.com/training/Authors/Details/julie-lerman If you sign up for the free trial you should be able to take in all of these F.O.C. Julie also has a regular column in the MSDN Magazine (‘Data Points’, which began in 2010); http://msdn.microsoft.com/en-us/magazine/ee310108.aspx And her own blog; http://thedatafarm.com/blog/ The Entity Framework team have their own blog; http://blogs.msdn.com/b/adonet/ And of course Entity Framework is now open source https://entityframework.codeplex.com/ Finally if you have problems with Entity Framework then one of the best places to seek help is Stack Overflow http://stackoverflow.com/questions/tagged/entity-framework That is but a small part of what’s available, there really is a wealth of information out there these days. There are also various third party tools for using Entity Framework (you wouldn’t be reading this if you didn’t have an interest in one of them). One worthy of mention here, because it makes model creation very easy, is Devart’s Entity Developer. http://www.devart.com/entitydeveloper/ If you only ever build tiny models comprised of less than ten entities it’s actually free to use!
Where do we go now?
Thereafter there will be additional improvements. If you want to proffer your ideas for things that you would like to see then we’d like to hear from you. The best place to do this is on the ComponentOne forums. Initially we’d suggest that you use the forum dedicated to the C1DataSource in the Winforms section (http://our.componentone.com/groups/forum/winforms/entity-framework-datasource/ ) where you can also ask any specific questions that you might have about the C1DataSource and its use in your projects.