Scaffhold views/controllers within n-tier solutions. EF Code first flavour.

I’ve been struggling for a while this morning, trying to use one of the most popular MVC tools features: the ability to generate a controller with the CRUD (Create, Read, Update, Delete) actions over a model. The generator will create the different views as well. Or better.. it should =) Everything works smoothly when you got one project only within your solution , including models, dal etc.

While if your app is layered into several projects, here’s a couple of things you should keep in mind in order to get the scaffholding working:

  • The project containing the model you want to scaffhold must include the configuration file with the connection string for your context.
  • The web project (from where you will create the scaffholded controller) not only it must have the config file with the connection string. The connectionstrings can’t be in separated config files (configSources)! Or you will end up getting this : The type initializer for ‘System.Data.Entity.Internal.AppConfig’ threw an exception. 

Last thing to keep in mind is to expose the DbSets instead of IDbSets in your DbContext or the code generator will not work. Happy scaffholding to everyone!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s