What I have been doing since.
Been a while since I posted here. Blame real life and some computer issues. Purchased a new laptop as spring with the intension of it being my new development machine and that I would make more use of virtual machines and such tricks.For the last month it has been unavailable to me as it was randomly crashing. I have been on the phone with Dell tech support several times for hours on end the issue is still unresolved but meanwhile I returned to my old machine which in spite of making the occasional funny fan noise it is still soldiering on. I rebuilt my database server on another machine and need to look into backup solutions as my 1TB NAS disk is filling up faster than I would like.
Exploration of Entity Framework
One of the things I have been researching recently is Entity Framework, this is mainly because i have been considering of applying it to a project i have been working on. However, I have noticed a couple of things on my explorations on the web; most of the code is in C# and the examples are either, trivial or very complex. So after some research I have decided to purchase a book. In general I think to learn better from books if I need more in-depth knowledge. So I choose “Entity Framework 4.0 Recipes – A problem-Solution Approach” by Larry Tenny and Zeeshan Hirani. Of course it is written in C#, one wonders is there a deliberate policy of deprecating vb.net or do they simply consider that all serious programmers are using C#. Anyway I decide it would be fun for me to rewrite all the listings in vb.net. This would serve two purposes; it would consolidate my knowledge of C# and learn some new tricks in vb.net. I even emailed Apress asking permission to list here, the vb.net translations of the code listings in the book (Never heard anything back – so I guess I won’t go there).So far I have just completed chapter 2 which covers various scenarios concerning the way different database table structures could be modelled in the Entity Framework. Chapter 2 covers the following scenarios:
- Creating a simple model
- Creating a model from an Existing database
- Modelling a Many to Many relationship with no payload
- Modelling a Many to Many Relationship with a payload
- Modelling a Self-Referencing Relationship
- Splitting a Entity across multiple Tables
- Splitting a Table across multiple Entities
- Modelling Table per Type inheritance
- Using Conditions to filter an ObjectSet
- Modelling Table per Hierarchy inheritance
- Modelling Is-a and Has-a relationships between two Entities
- Creating, Modifying and Mapping Complex Types.
Now I am not going to create tutorials on Entity Framework. I don’t yet know enough about the topic and the basics are well covered on MSDN, what I intend to do in this and future posts is create a guide to the “Entity Framework Recipes” book mentioned above. So basically I’ll list the topics covered in a chapter and include some code, the original C# listing and my vb.net translation.
So if we take the data tables shown in below.
So the business table is a base set of information and the Retail and eCommerce tables hold additional information that relate to that type of business.
When you initially create a new Entity Framework mapping you get a similar set of entities.
Here we have the same tables as Entities in the Entity Framework mapping created by default. The relationships will be edited and replaced by inheritance relationships.
Delete the existing relationships and replace with inheritance relationships. Also remove the primary key entries in the inherited entities and then check that the mappings are still correct.
The final version of the used in the application looks like this.
The C# code listing to write the data to the database and then retrieve it and display it to the console looks like the following code.
using (var context = new EFRecipesEntities()) { var business = new Business { Name = "Corner Dry Cleaning", LicenseNumber = "100x1" }; context.Businesses.AddObject(business); var retail = new Retail { Name = "Shop and Save", LicenseNumber = "200C", Address = "101 Main", City = "Anytown", State = "TX", ZIPCode = "76106" }; context.Businesses.AddObject(retail); var web = new eCommerce { Name = "BuyNow.com", LicenseNumber = "300AB", URL = "www.buynow.com" }; context.Businesses.AddObject(web); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("\n--- All Businesses ---"); foreach (var b in context.Businesses) { Console.WriteLine("{0} (#{1})", b.Name, b.LicenseNumber); } Console.WriteLine("\n--- Retail Businesses ---"); foreach (var r in context.Businesses.OfType<Retail>) { Console.WriteLine("{0} (#{1})", r.Name, r.LicenseNumber); Console.WriteLine("{0}", r.Address); Console.WriteLine("{0}, {1} {2}", r.City, r.State, r.ZIPCode); } Console.WriteLine("\n--- eCommerce Businesses ---"); foreach (var e in context.Businesses.OfType<eCommerce>) { Console.WriteLine("{0} (#{1})", e.Name, e.LicenseNumber); Console.WriteLine("Online address is: {0}", e.URL); } }
The same code in VB.Net
Using context As New EFRecipesEntities Dim business = New Business With {.Name = "Corner Dry Cleaning", _ .LicenseNumber = "100x1"} context.Businesses.AddObject(business) Dim retail = New Retail With {.Name = "Shop and Save", _ .LicenseNumber = "200C", _ .Address = "101 Main", _ .City = "Anytown", .State = "TX" _ , .ZIPCode = "76106"} context.Businesses.AddObject(retail) Dim web = New eCommerce With {.Name = "BuyNow.com", _ .LicenseNumber = "300AB", _ .URL = "www.buynow.com"} context.Businesses.AddObject(web) context.SaveChanges() End Using Using context = New EFRecipesEntities Console.WriteLine(ControlChars.Lf & "--- All Business ---") For Each b In context.Businesses Console.WriteLine("{0} (#{1})", b.Name, b.LicenseNumber) Next Console.WriteLine(ControlChars.Lf & "--- Retail Business ---") For Each r In context.Businesses.OfType(Of Retail)() Console.WriteLine("{0} (#{1})", r.Name, r.LicenseNumber) Console.WriteLine("{0}", r.Address) Console.WriteLine("{0}, {1} {2}", r.City, r.State, r.ZIPCode) Next Console.WriteLine(ControlChars.Lf & "--- eCommerce Business ---") For Each e In context.Businesses.OfType(Of eCommerce)() Console.WriteLine("{0} (#{1})", e.Name, e.LicenseNumber) Console.WriteLine("Online address is: {0}", e.URL) Next End Using