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!

No Comments

You can leave the first : )

Leave a Reply

Your email address will not be published. Required fields are marked *