Month: April 2017

Entity Framework – Code First DB migration

Last Updated on January 30, 2021 by sandeeppote

Code first is useful in Domain Driven Design.Code first use Convention based migration- it is a set of rules to automatically configure a conceptual model based on domain class definition.
Type Discovery- Code first approach generates classes for the properties set in DBSet and also includes referenced types that are included in classes, even if referenced types are defined in another assembly.
The conventions for the type discovery are:

  1. Code-First includes types defined as a DbSet property in context class.
  2. Code-First includes reference types included in entity types even if they are defined in different assembly.
  3. Code-First includes derived classes even if only the base class is defined as DbSet property.

Assuming entities are created and DBContext is set along with the DB Connection string to enable migration use –
enable-migration command in package manager console.once the migrations are enabled Migrations folder is created with the InitalCreate.cs class. Any further changes to the model a version of changes are created with a Up() and down() method which helps to Migrate and Rollback the model changes to database.
To update the last changes to the DB use add-migration “migration title” and update-database command in console.This should now update or create Initial tables and changed schema. Along side this a Seed() method is called. You may need to add the initial/sample data to tables use the context for same in Seed() method.
To Rollback the changes use update-database -targetmigration “migration title”. I find this a bit confusing initially but what this command does is it takes back the database state to the last best migration performed rollingback the latest changes performed.
While doing so you may receive error-Unable to generate an explicit migration because the following explicit migrations are pending: [201704251318501_migrationname]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
This seems to complaining about the migration which are performed where a version is missing. So migrations has to be happen in a sequence of it was created.

Hope this helps.

Asp.Net MVC Attribute Routing

Last Updated on January 30, 2021 by sandeeppote

Asp.net MVC Routing determines the appropriate actions, controllers and  data for any incoming request specified in a format or a group of format sent to parse the URL’s.Routing rather than having concrete physical files uses abstract actions.


This is achieved mostly by Convention Routing. Asp.net MVC 5 as another scheme called Attribute Routing. Attribute routing helps to keep the routing definition at the close proximate of actions and controllers. This similar to Convention Routing adds the routes to Route Table. 

Enabling Attribute Routing

Use MapMvcAttributeRoutes() as shown in below code sample. This enables both Convention and Attribute routing

 public static void RegisterRoutes(RouteCollection routes)  
     {  
       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
       routes.MapMvcAttributeRoutes();  
       routes.MapRoute(  
         name: "Default",  
         url: "{controller}/{action}/{id}",  
         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
       );  
     }  

Following example shows usage of Attribute Routing :-

 [Route("SingleStringParam/{FirstParam}")]  
     public ActionResult OneStringParameter(string FirstParam)  
     {  
       ViewBag.FirstParameter = FirstParam;  
       return View();  
     }  

For nullable parameters use ? in front of parameter:-

[Route("SingleStringOptionalParam/{FirstParam?}")]  
     public ActionResult OneStringOptionalParameter(string FirstParam)  
     {  
       ViewBag.FirstParameter = FirstParam;  
       return View();  
     }  
   }  

What is better – Convention or Attribute routing, my opinion-
Attribute routing is more flexible than Convention routing as it is easy to maintain and close to the actions and controllers. Instead of managing whole list of routes in route.config, tedious to maintain, use attribute routing. As attribute routing are close to Controller Action it helps in debugging and troubleshooting, as well as ability to quickly search the route information in your solution.

Reference- Attribute Routing