A Few MVC SQL Tips and Tricks

I have been working with Visual Studio 2015, MVC .NET and MS SQL lately, and ran into several things that I had to keep looking up over and over, so I thought there may be someone else out there in the same boat.

Note: These are all tips for working code-first in Visual Studio.

  1. In your Model, say you want to have a table’s Primary Key auto-increment. Sure you add [Key], but you also need to add this: [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    auto_increment
  2. Here is a handy snippet you need to add to Global.asax in order to add more than one controller to your project:
    Database.SetInitializer<YourProjectContext>(new DropCreateDatabaseIfModelChanges<YourProjectContext>());
    In my screenshot, the name of my project is IceCream. If you don’t know the name of your project context, it should just be the name of your project with “context” on the end. I know that’s pretty basic, but as a new coder, it sometimes throws me. Global.asax is located at the very bottom of your Solution Explorer.
    if_model_changes