Thursday, December 24, 2015

How to configure one-to-one relationships with Entity Framework and Code First

Configure One-to-One Relationship - this site is focused on EF in general, but also has this specific article.

Must have Graphics Tools

Below is a short list of some really great tools for editing or creating graphics, editing photos, etc.

The Gimp - It is the cross platform FREE and Open Source alternative to  Photoshop.

Paint.NET - This is my long time favorite because it is easier to install and use than Gimp, opens faster, and has most the features I need.

Pixlr - Has most of the important functionality of The Gimp, but runs in any browser that supports flash.

Please suggest any other graphics apps that should be included here.

Tuesday, December 22, 2015

Map Windows Share to a Drive Letter using batch file / command line and prompt for password

The line of code can be pasted on a command prompt in Windows 2000 and newer or pasted into a text file with a .bat extension. It will prompt the user for a password to the Windows share. The username is hard coded

rem net use /del V:
SET /P %password%=Password:
net use v:
\\Wtjwh175\c$\dev\dropbox /u:"yourDomain\yourUsername" %password%

Technically, you will get an error saying "The local device is already in use.", if you run it twice. If you don't want that to happen you can uncomment line 2 (change rem net use /del V: to net use /del V:). This will unmap the drive and then map it again each time the batch file is executed.

Thursday, December 3, 2015

Passing additional view data to a Edit to make sharing a EditorTemplate on an Edit and Create view.

Imagine you have a Editor in Shared\EditorTemplates\Person.cs for an object (Person in this example) and two views for an Order entity that is showing edit fields for Order obeject and the Person object. The two EditorTemplates are Person\Edit.cshtml and Person\Create.cshtml. To use the Person EditorTemplate in the Order Edit AND Create view we need to make sure we always pass a value for the Person.OrderID. If we don't do this, the problem is that when we use the editor in the context of the Order\Create.cshtml View the Model is null and validation for the hidden field will have a value of null. When it is on Order\Edit.cshtml the model will not be null and we will have a value for model (a Person object) and thus have the value Model.OrderID. It is really the Create that requires this work around, but we want to be able to use the EditorTemplate even when it is on a Create view. Below is the code needed

Code located in the Order\Edit.cshtml View
model in this context is the Order object
NOTE: We ARE passing MyOrderID value here since we do need the editor to know and keep the value.

@Html.EditorFor(model => model.Person, new { MyOrderID = Model.OrderID })

NOTE: When we set MyOrderID, it is scoped to the EditorTemplate that we are passing the value to. However, if we put the value in the ViewBag or ViewData directly (at the top or Order\Edit.cshtml for example) then it is available to all Views and EditorTemplates in this context.

Code located in the Order\Create.cshtml View
model in this context is the Order object
NOTE: we are not passing the MyOrderID value here since we just need any value (-1 was arbitrarily chosen to be safe) and will be assigned by the Entity Framework when it is saved to the database. Most importantly it will pass the validation and the model will be valid until then.

@Html.EditorFor(model => model.Person)
Code in the partial view ( in EditorTemplates folder makes it a template) for the Person object.
model in this context is the Person object
NOTE: If no value is passed using ViewBag.MyOrderID then we use -1, otherwise we use the passed value (usually from the Order\Edit.cshtml View)

@Html.HiddenFor(model => model.OrderID, new { Value = ViewBag.MyOrderID ?? -1 })

You can also use a strongly-typed ViewModel and pass all data to it instead of using the ViewBag.

Wednesday, December 2, 2015

Configure Code First migrations to use Singular Table Names

If you are using Code First Migration with Entity Framework you will probably have noticed that the table names it generates are all plural. Personally, I dislike this because all the contstraints including foreign keys are very confusing to read when they are plurarlized. Before you do an update-database for the first time be sure to override a method (OnModelCreating()) on your class (typicaly ApplicaionDbContext) that inherits from IdentityDbContext. The default class is called ApplicationDbContext.

public class ApplicationDbContext : IdentityDbContext
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)

        public static ApplicationDbContext Create()
            return new ApplicationDbContext();

        public DbSet People{ get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
            // make the table names singular instead of plural
            //when code-first generates the table names



Tuesday, December 1, 2015

Review of some products for working with MS Word documents using .NET / C#

I didn't want to use MS Automation because it is slow and not server friendly, so I am not covering that here. The solutions below do NOT require MS Word to be installed and they are server friendly.

Open XML Word Processing SDK
This is my first choice for completely free solutions because is relatively easy for basic things and it will likely always be supported by Microsoft. This is the Microsoft SDK (released as Open Source as of version 2.5) for creating and editing MS Word documents in DOCX format. It takes a bit to get used since it mirrors the internal DOCX format. The advantage is that pretty much everything you need to be done here. The disadvantage is that it may take more code than using other packages. The link above has sample code on how to do many common tasks. Here is a good place to start to familiarize yourself with the file formats, what resources are available, etc. To understand the DOCX file format, check this out. The DOCX format is much simpler than the XLSX format. I recommend giving it a try. The learning curve is pretty small for the DOCX file format and there is a lot of examples available. In the end it isn't so different than NPOI (see section below) for DOCX. Here is a list of how to do's.

It is unofficially distributed on NuGet as DocumentFormat.OpenXml, but it is unofficially distributed by someone other than Microsoft (even though it shows the author as Microsoft). If you want the official binaries they can be downloaded here. If you want the source code you can get it on GitHub. This video shows how to build the source code if you want to go that route. If you want to use 2.6 it has been released on NuGet as OpenXMLSDK-MOT. This is the same as official binaries and source code in GitHub (from what I can tell). All options install an assembly called DocumentFormat.OpenXml and if done through NuGet it is added a reference to your project.

If you want to validate that the document you create is valid, check out this code example.

Here is an example of how to do a search and replace in a DOCX file.

Here is the link to the Open Xml Developer website.

There are also tons of example on the Open Xml Power Tools project on GitHub and from NuGet.

Open-Xml-Power-Tools - DocumentAssembler
This is an Open Source module in Open Xml Power Tools that allows you to create templates using .DOCX files as templates, a xml data source, and generating a well formatted DOCX file with the data merged in to the template. The template allows such things as tables, conditions, etc using XPATH-like syntax. Here is a video on how it works, but not a step by step tutorial. Here is a getting started video / tutorial that walks you through using the product. It is important to watch the ending where he talks using <# #> instead of Content Controls in Word as the placeholders. It works similar to most reporting tools, but using MS Word as report definition (template) file and the output being a MS Word document as well. You can download the entire Open-Xml-Power-Tools suite that includes DocumentAssembler from here. It is not meant to be used to create DOCX documents from scratch. It always uses a template file to generate new DOCX files. However, Open-Xml-Power-Tools suite does have the ability to help work with DOCX files. NOTE: It is built on top of (requires it) the Open XML Word Processing  SDK (above).

If you really don't want to learn anything about the DOCX format and want a more inuitive way to interact with the DOCX files then this may be a good option if you are okay with alpha software. A simple Open Source project available from or NuGet. It let's you interact with the Word document in an intuitive manor without understanding how Word documents internally work. The examples on his blog are good and good examples in source code. It is easy to use. It is still alpha, but has been around since 2009. It has growing popularity.

Free Spire.Doc
This is a free version with limitations for a professional package called Spire.Doc. It is quite powerful and also does conversions to PDF and many other formats. It does mail merges, etc. Is is nice for small projects that are below the limitations. The API is well thought out and works nicely.

This is the .NET implementation of the popular POI Java Project. It is Open Source and totally free and also does MS Excel. The API is a bit low level at time, but works well. I recommend downloaded from github to work with examples. Also, you can install binaries via NuGet. The documentation and examples for XDOC are not really there yet though. Look for XWPF if you want to use DOCX file format. The problem is that it is not the exact same API as the POI Java Project, but it is similar. It is also not as mature and is thus missing some features. It is a bit more abstract that just using the Microsoft packages, but still quite a bit of DOCX format knowledge is needed. In general I found the experience a bit frustrating because of how the project is organized and the poor documentation and very few DOCX examples.