Using EF code first with an mdf database in App_Data

I’ve been playing around some more with EF code first. Today’s task was to try to get it to work with a .mdf file located in the App_Data folder of the web project.

It wasn’t much fun.

I hijacked the existing aspnetdb.mdf and tried to get that to work with EF Code First. Short answer: don’t bother, for these reasons:

  1. I couldn’t get EF code first to create my Category table
  2. So then I’d created the table manually myself in SQL Server Express, but EF still wouldn’t recognise it – “Invalid object name dbo.Category”
  3. So then I decided to start with a new .mdf file and yay EF code first created my Category table
  4. run aspnet_regsql.exe on it to get the ASP.NET Membership tables in there – fail
  5. Visual Studio has a lock on the mdf file – delete it from VS’s Server Explorer, run aspnet_regsql, yay
  6. Open DB in SQL Server Management studio to see what my database looks like – fail, locks on file

I’m not sure how to debug EF code first, I mean if it doesn’t work (i.e. your table isn’t created) you have no idea why.

So I guess I got it working in the end, but all the problems with the files being locked made me realise it’s not worth the hassle so I started again with a normal database inside SQL Express.

  1. Add your connection string:
    <add name="SiteDB" 
         connectionString="data source=.\SQLEXPRESS;Initial Catalog=Site;Integrated Security=SSPI;"
         providerName="System.Data.SqlClient" />

I recommend explicitly setting a connection string otherwise your database will have a big silly namespaced name. You don’t need to create the database though, EF will do that much for you.

  1. Run your app and hit the Categories page so that EF will create the tables
  2. Run aspnet_regsql.exe on it to add the membership tables
  3. Change your membership connection string to point to the same DB – or tell the AspNetSqlMembership provider to use that connection string blah blah

Announcing MVC3 starter site

Inspired by Rob Conery’s MVC starter site, I’ve created an MVC3 starter site.

It basically takes Visual Studio’s File -> New Project -> ASP.NET MVC 3 Web Application and builds on that, adding all the things we need to get started – IOC (Unity), Unit of work pattern, Logging (NLog), Reporting, oh and some CSS from freecsstemplates.org.

It’s also my first open source project in a while…

You can download it from codeplex at http://mvc3starter.codeplex.com

mvc3 starter app screenshot

Organise your digital life – Part 1 Photos

I got my first digital camera in 1998. It was a low-end Kodak model but it was still quite expensive, around US$200, and it took photos at a whopping 493×373 resolution. I think it held about 20 photos in it’s internal memory. It didn’t even have a flash!

Back then I didn’t have much of a strategy for organising my digital photos – and the photos were so crap I didn’t use it all that often anyway:

My student workspace in 1998, taken on my 1998 era Kodak digital camera. Spray-painted keyboard and spray-painted big-ass server case, oh yeah. Looks like the 14″ monitor is running Winamp.

My next digital camera was in 2003 – a Pentax Optio S, the original “Altoids tin” camera. This camera served me well and I still have it lying around somewhere. Alas the flimsy battery compartment lid broke off but other than that it still works.

Pentax Optio S

Anyway, about 2004 I realised I needed a strategy for organising all of my photos, and the strategy I chose and which have stuck to is this.

Under My Pictures I have a Camera folder. In the Camera folder is a folder for each year:

Each year is divided up into sub-folders named after the locations where I have taken photos.

The naming format of each subfolder is “## – Place name”. E.g. in 2010 the first place I took photos was at my friend Kelvin’s wedding in New Plymouth so that is the first folder listed.

It’s really quite straightforward (and I think worthwhile) to organise your photos into folders based on year and location.

Finally, at the photos level is where I get a bit more pedantic (and it’s probably not necessary).

I usually rename my folders “Event – ##” or if the photo contains people then “Event – ## – Person Name”. I use a tool for renaming the photos in this way (an old version of ACDSee that came bundled with my Pentax camera in 2003) but I still need to manually add my friend’s names to the filename.

And that’s it. I know that most people aren’t going to bother to rename each photo the way I do (fair enough), but I think it’s not much effort to organise your photos into folders by year and location. And for god’s sake, delete your crap photos. I am notoriously economical with photos, i.e. I might take 10 or 20 shots of a landmark but I’ll only keep 1 or 2 of them.