;

DbContext, POCO templates and codefirst-mode

Posted : Sunday, 29 April 2012 09:44:00

I use Entity Framework a lot these days, I’m not saying its perfect but its a come a long way. Its definitely a lot easier to get up and running than NHibernate and this is thanks in no small part to the tooling available from within the Visual Studio IDE. Typically I’ve found my usual “new-application workflow” to consist of the following steps

  1. design a set of entities using the model-first approach
  2. use entities from step 1 to generate a database
  3. add ADO.NET DbContext Generator template to convert the entities to POCO classes
  4. add view layer

While step 3 isn’t totally necessary, without it all the entities will inherit from EntityObject and any part of the application that uses them will need a reference to the entity framework and that just isn’t right!

So when you perform step 3 you get a T4 generated DbContext derived class that will look something like this….

    1 //------------------------------------------------------------------------------

    2 // <auto-generated>

    3 //    This code was generated from a template.

    4 //

    5 //    Manual changes to this file may cause unexpected behavior in your application.

    6 //    Manual changes to this file will be overwritten if the code is regenerated.

    7 // </auto-generated>

    8 //------------------------------------------------------------------------------

    9 

   10 using System;

   11 using System.Data.Entity;

   12 using System.Data.Entity.Infrastructure;

   13 

   14 namespace CodeFirstMode

   15 {

   16     public partial class CodeFirstModeModelEntities : DbContext

   17     {

   18         public CodeFirstModeModelEntities()

   19             : base("name=CodeFirstModeModelEntities")

   20         {

   21         }

   22 

   23         protected override void OnModelCreating(DbModelBuilder modelBuilder)

   24         {

   25             throw new UnintentionalCodeFirstException();

   26         }

   27 

   28         //DbSet<Entity1> Entities1

   29         //DbSet<Entity2> Entities2

   30         //....etc

   31     }

   32 }

   33 

Notice the UnintentionalCodeFirstException, googling this doesn’t reveal much other than…

“The exception that is thrown when a context is generated from the DbContext templates in Database First or Model First mode, and is then used in Code First mode. “

But what is code-first mode and how can I switch between modes. Up until recently this has been only an idle curiosity but I was working on a project where the model development was mostly complete and so not changing very much. I needed to hook into the OnModelCreating routine with a model that had been generated using the DbContext POCO template. Easy enough right? Wrong, I couldn’t find anything anywhere telling me how to switch to code first mode. My assumption was that once I found this undocumeted switch I could apply it, then alter the T4 template to add the code I wanted into the OnModelCreating function (obviously removing the UnintentionalCodeFirstException).

After a bit of poking around I found the answer, it occurred to me that when using code-first from the outset, generally you add a simple connection string to the config file for your application and go from there whereas if you start from the EntityFramework model item templates, an entity framework connecton string gets added (eg the ones with the path to the CSDL, SSDL & MSL as well as the actual database connection string itself. I changed the connectionString property to a regular connectionstring and providerName from “System.Data.EntityClient” to “System.Data.SqlClient”. I reran the application and Bosch! an UnintentionalCodeFirstException.

So to switch into code-first mode just change the connection string from the entity framework specific flavour to a regular vanilla connection string. If the model is quite complex then the fluent mappings will need to be set up correctly manually, if you get stuck a look in the .edmx file can give some clues.

  • (This will not appear on the site)

The codeface gets a facelift

Posted : Friday, 06 April 2012 15:15:00

When I originally started this blog way back in 2009 I wrote is a learning aid and an introduction into the world of MVC.Net. At that time I wasn’t too bothered about the general appearance of it. It was OK, a bit plain but did the job. Over my career I’ve found its very important to know your strengths and equally important to know your weaknesses, on that note, visual design is a great weakness of mine. Like many developers I guess, my approach to design goes something along the lines of “centre align, arial font, bit of hex colour background and bosh – job done!”. I’ve met many designers over the years and am always amazed by how they come up with such variety and new ideas for what is, in the case of web-design, essentially a few hooks on the page to hang stuff from. I’ve met a few developers who have a degree of disdain, even contempt, for designers deeming design as simple as “colouring-in” or worse. While I would agree that generally there is more work involved in coding an application that designing the interface I would certainly contest that either is easier more important than the other. After all just look at the success of Apple, this is in no small part to their marrying exquisite design with well built application interfaces. My analogy to the design verses development debate goes like this.

An application is like a cake. The developer is the baker, turning his or her skills to blending the right mix of ingredients (patterns, practices, architecture) to make a beautiful tasting foodstuff. The designer on the other hand is responsible for icing the cake, creating such a visually appealing visage for the cake as to have the consumer (user) salivating before they’ve even taken a bite. So imagine the case where this delicious cake has been iced by someone with little skill and no care or attention for the end product. It doesn’t matter how good the cake might actually taste – if it looks horrible you ain’t gonna want to eat it!

I’ve been meaning to overhaul TheCodeface for a while now and while I was chatting online to a designer friend I used to work with called Louisa and I asked for a quote – she gave me a very reasonable price so I commissioned her to overhaul TheCodeface. As I said before I’ve worked with quite a few designers over the years and the ones that I remember most are the ones who its easy to work with from development perspective – The reskinning of TheCodeface was a breeze thanks in no small part to Louisa’s understanding of the development process and the point at which design and development meet (separation of concerns to use zeitgeist jargon). After a bit of toing and froing approved flats were converted into a series of html, css, images and Javascript files which I then incorporated into the site. I stipulated at the outset that I wanted a nice design that looks as good on mobile as it does on laptops and desktop and that’s what I now have, the whole process was quick easy and I am really happy with the results!

If you’re looking for a skilled designer go check out her website at CLK Digital.

  • (This will not appear on the site)

SQL Server Analysis Services in Windows Azure

Posted : Friday, 30 March 2012 14:59:00

Recently I was investigating on behalf of a client, possible hosting strategies for an SSAS based BI system. One option that would seem to be quite favourable would be to host SSAS within the Azure cloud – one would think that this would be a logical option; In my experience Microsoft products play really nicely together so it would seem reasonable to assume that SSAS would sit well within the cloud model, offering really low setup costs for customers wishing to start out on an OLAP journey without the significant financial budgets that are often a barrier to this type of project. It therefore came as quite a surprise that Microsoft currently have no plans to offer Analysis services as part of the SQL Azure product. I had a chat with a few people and thrashed out what could be an Azure based SSAS architecture. The diagram below is a high level representation of this. It should be stressed that this hasn’t been attempted so if you do attempt this, do so with caution. That said if you do go for it, and it does work, please do let me know!

Possible SSAS Azure deployment

  • (This will not appear on the site)

Recently read, highly rated...

previous next