mongodb as a service

Download and Install Mongo db for your OS from

Created a folder to store the collected data in mongodb
e.g.:- C:Path\mongodb

Create a log folder in mongodb folder and create mongodb.log file.

Execute the below in command from the path where mongo is
installed or set the global variables to execute this command.
Update the path accordingly

mongod --dbpath "C:\Path\mongodb"
--serviceName "LocalMongoDB"
--logpath "C:\Path\mongodb\log\mongodb.log" --logappend --install

You should see in you serives (run services.msc) mongo service
which will be in stopped state. Start the mongo db service.
You may start monngodb from command prompt.
For more details see this -

Entity Framework - Code First DB migration

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.

VS 2017 and C#.0 new features

Attribute Routing 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. 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)  
         name: "Default",  
         url: "{controller}/{action}/{id}",  
         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  

Following example shows usage of Attribute Routing :-
     public ActionResult OneStringParameter(string FirstParam)  
       ViewBag.FirstParameter = FirstParam;  
       return View();  
For nullable parameters use ? in front of parameter:-
     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.

Sitecore project - Fix broken intellisense

IntelliSense provides code completions based on language semantics and an analysis of your source code.
IntelliSense suggestions popup as you type. This might trouble you whilst setting up Sitecore new project. 

It comes with the following error-
Error CS1061 'HtmlHelper<RenderingModel>' does not contain a definition for 'Sitecore' and no extension method 'Sitecore' accepting a first argument of type 'HtmlHelper<RenderingModel>' could be found (are you missing a using directive or an assembly reference?)

Following can be checked if the Sitecore intellisense is not poping up-
1. Check if Sitecore.Mvc namespace is added in web.config. Namespace should sit in  <pages pageBaseType="System.Web.Mvc.WebViewPage"><namespaces>
2. Check if web.config file is in View folder. If not, you might need to add this.
3. Follow same as Point 1 for web.config file in View folder.
4. Next you must have referenced Sitecore.Mvc dll in the project. Set the Copy Local to true.
5. Rebuild solution and re-open the cshtml file(s).

The intellisense should now be working.

Error whilst Reverse engineer code first using EF power tool

If you receive following error - change project Target Framework to 4.5.1.

One or more errors occurred while processing template ''. error : An exception was thrown while trying to compile the transformation code. The following Exception was thrown: System.ArgumentException: Empty path name is not legal. at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.IO.File.OpenRead(String path) at Roslyn.Utilities.FileUtilities.OpenFileStream(String path) at Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(String path, MetadataReferenceProperties properties, DocumentationProvider documentation) at Microsoft.VisualStudio.TextTemplating.CompilerBridge.<>c.<.ctor>b__15_0(String x) at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext() at System.Linq.Enumerable.d__66`1.MoveNext() at System.Linq.Enumerable.d__66`1.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items) at Microsoft.CodeAnalysis.ImmutableArrayExtensions.AsImmutableOrEmpty[T](IEnumerable`1 items) at Microsoft.CodeAnalysis.Compilation.ValidateReferences[T](IEnumerable`1 references) at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.WithReferences(IEnumerable`1 references) at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.CommonWithReferences(IEnumerable`1 newReferences) at Microsoft.VisualStudio.TextTemplating.CompilerBridge.PrepareNewCompilation() at Microsoft.VisualStudio.TextTemplating.CompilerBridge.Compile() at Microsoft.VisualStudio.TextTemplating.TransformationRunner.Compile(String source, String inputFile, IEnumerable`1 references, Boolean debug, SupportedLanguage language, String compilerOptions)

EF Power tool for VS2015

EF power tool downloadable from here currently are not compatible with VS 2015. To make it compatible perform following steps-
2. Rename file EFPowerTools.vsix to .zip
3. Open extension.vsixmanifest file and add following 

4. Save changes and Rename file to .vsix
5. Install tool

You should now be able to see the Entity Framework option on right click of your project.

Hope this helps