Category Archives: .Net

Entity Framework generate script for specific migration (aka EF Cheat Sheet)

I’ve sometimes needed to generate a specific SQL Script to apply a specific Entity Framework migration to my database.

This post is mostly for my record of how to do this, and here is the link to where I found the solution. It’s near the bottom of the page.  One day I will re-write the instructions here in a cleaner format, but for this will help me find it again, and maybe anyone else who is looking.

Here’s the link: entity-framework-migrations-cheat-sheet

Quick Peek at Visual Studio LightSwitch

I’ve been curious about Visual Studio LightSwitch for a while. It’s a rapid application development tool from Microsoft which lets you quickly develop HTML/Javascript CRUD apps and small business apps for web browsers and mobile devices (I think it can do desktop apps too, but it uses Silverlight to do it, and I’m only interested in web apps these days.)  LightSwitch is installed out-of the box in Visual Studio 2013 Professional and the new (free) Visual Studio Community 2013 edition.

Note: This is not a deep-dive into LightSwitch. It’s just a quick 5 minute review of what I accomplished in less than 30 minutes piddling around with this thing one evening to get a peek at what it’s like. Before I got started, I had watched this nice video on YouTube and it really helped me jump right in:

I recall hearing at one point, when LightSwitch first came out, that some people proposed it as an alternative to Visual FoxPro, or at least a path forward for FoxPro devs who had never built web apps before or did not want to take the full plunge into the .Net world.  So, I’m curious if any of my FoxPro peeps have taken up with this? If so, let’s hear from you.

Here we go… Basically, I found it dirt simple to get up and running. I just created a new LighSwitch prject in Visual Studio and pointed it to a database in Sql Server Express running on my local developer machine (I already had Sql Server Express installed for other .Net work). Next I picked which tables I wanted to work with in the app. You then add views for any given table, selecting which fields to show.  The initial page layouts are auto-generated for the selected fields, and you’ll quickly have a live list view and edit page running in your browser.

I’ve built real web apps in Ruby on Rails and Asp.Net MVC with Entity Framework, and I’ll tell you hands down from real experience that getting a simple CRUD page alive in LightSwitch is much  easier than in either of these other two platforms. Now, I’m betting there are some real limitations that come with using a simple application framework like LightSwitch. So, I don’t dare suggest that an app built in LightSwicth can compete with all the features needed to build a complex business app. You’ll have to learn more about that for yourself. However, there are lots of examples and forums where you can learn how to include advanced UI or backend code when needed.

Here’s a simple edit page. Just make a change and click the little Save icon in the upper right corner of the screen:


To create or edit the views, you use a treeview designer-like UI in Visual Studio to create rows, columns, groups, fields, as well as a command bar for action buttons and other UI controls. It adds default label captions above each field, but you can override those as well as field widths and other styling options in the Properties window (see blow).


Here is a view of the simple Property Editor showing the various properties you can tweak for each control on the page layout.



Ok, it’s all up to you from here. Watch the video linked above, and try it out for yourself.

Next Step

The next three things I plan to explore:

  1. How it handles lookups, like when you need a dropdown list to choose a value from a lookup table and store the ID in a local field.
  2. Client side, or backend validation.
  3. Showing related child records in a grid on the same page as the parent detail fields.

If my interest is there, and my time allows, I’ll update this page if I accomplish anything with these items above.


LightSwitch on MSDN: (lots of how-to-get-started content.)

MSDN Forum for LightSwitch:

PluralSight courses on LightSwitch:

Free video:

LightSwitch articles on CODE Magazine website:

WPF Hole Patterns

After I got my head above water in C# and WPF back in 2008, I created and published a sample of my first C# work that used the WPF platform for the UI. See my WPF Hole Patterns application on CodePlex to take a look at some of the vector graphics that are possible with C#, as well as how clean and pretty a WPF UI can be.

WPF Hole Patterns app is a vector based, CAD-like geometry program for laying out circular and linear hole patterns. In all, between the app and the source code, I’ve had over 100 downloads, so hopefully, it’s been helpful to some curious developers to see what they can learn from my efforts. I’ve since ported the app to Silverlight and used it as a practice field for learning the basics of the MVVM pattern. If you’d like the latest version in Silverlight, just drop me an email.

Another Example

You can also another great example (much better than mine) by checking out this Silverlight Live Geometry app on CodePlex. It’s another vector based drawing program intended to show off the Silverlight vector canvas. You can find a live version of the app at the Codeplex site, or here:

Both apps include full source code in C# and can be a helpful too in learning some geometry-based programming in C#, as well as WPF and Silverlight drawing.

Using Linq to count rows in a DataTable based on a field value

// Using Linq To DataSet to count rows based on a filter
// compared to
// Using the plain old Select() method that's been on DataTables since the beginning.
// Learn more about Linq To DataSet here:

using System;
using System.Linq;
using System.Data;
using System.Data.Common;

namespace ConsoleApplication
 class Program
  static void Main(string[] args)
   // Build up some sample data in a DataTable...
   DataTable dt = new DataTable();
   dt.Columns.Add("nMenuLevel", Type.GetType("System.Int32"));
   dt.Rows.Add(new object[] { 1 });
   dt.Rows.Add(new object[] { 1 });
   dt.Rows.Add(new object[] { 2 });
   dt.Rows.Add(new object[] { 3 });

   // Here's one way using Linq
   int linqCount1 = (from DataRow row in dt.Rows where (int)row["nMenuLevel"] == 1 select row).Count();
   Console.Write("linqCount1 = " + linqCount1.ToString() + "\n\n");

   // Here's another way using Linq
   int linqCount2 = dt.AsEnumerable().Where(c.Field("nMenuLevel") == 2).Count();
   Console.Write("linqCount2 = " + linqCount2.ToString() + "\n\n");

   // Here's a non-Linq way, using the old Select() method thats been available on DataTables
   // since the beginning. It's acutally much shorter and easier to read.
   // But watch out! Notice this Select syntaxt only requires 1 equal sign, not 2.
   int countWithSelect = dt.Select("nMenuLevel = 3").Count();
   Console.Write("countWithSelect = " + countWithSelect.ToString() + "\n\n");

LINQ to SQL, Lazy Loading and Prefetching

I want to recommend this interesting article that explains times when LinqToSql’s native behavior of “Lazy Loading” is not a good thing (from a database performance standpoint), and explains a pre-fetching technique that you can can use when you want to avoid lazy loading hits to SQL Server.

It’s from the the great Rick Strahl. See his blog post at:

I also recommend that you review his LinqToSql Business Object wrapper framework and sample app that he created to show a real-world working app using the framework.

Original article and download, September 27, 2007 (read this first):

He also posted an update article on February 05, 2008:

And, Rick has continued to update the LinqToSql Business Object wrapper framework since those articles were written, and you can download the latest version which is included as part of the WestWind Web Toolkit for Asp.Net (tons of other goodies are in the entire Toolkit, so check out the free license details, which is a full working version) :


In closing… You know, they’ve said that LinqToSql was kind of dead, but at least they did do a little work on it for .Net 4 framework. That’s encouraging to me. I think a LOT of people use LinqToSql, and I personally think it will pick up even more, rather than die off. It has a fine place in small-to-mid sized apps, and I frequently hear plenty of notables in the community speak of it.

Here’s is an article on the updates to LinqToSql that included in the .Net 4 framework:

Uninstalling Visual Studio 2010 Beta 2

Why am I writing about Un-installing Visual Studio Beta 2 when it was just released yesterday, October 19, 2009? (video announcement can be seen at this link). Well, in the past whenever I’ve used the beta versions of some of Microsoft’s developer tools, I’ve found that it was impossible to uninstall them properly (or fully) when the RTM version was finally released.

Now, I know that VM’s can be used to test out beta software, and I do use VM’s on my machine, as I did when I installed VS2010 Beta 1 on a Windows 7 Beta VM running on VirualBox VM.  But sometimes I still like running right on the bare metal so I will have access to the rest of the tools I need in a full dev environment. Especially now that it’s Beta 2, I’d feel pretty good running it on my live dev machine. But, I was unsure about being able to easily remove it later when the RTM version comes out on March 22, 2010.

So, I posted a question on the Visual Studio 2010 Install and Setup forum of the newly restyled to see if anyone could address this issue.

My question is titled Will we be able to un-install VS2010 Beta 2, when final version is released?.

Fortunately, I got a very quick reply from a MSFT moderator named Aaronru who claims a very easy and successful uninstall of VS2010 Beta 2 has been tested and WILL allow for an easy upgrade path to VS2010 RTM.

The exact reply was…

For helpful instructions on how to install Visual Studio, we’ve published some detailed instructions for installing and maintaining Visual Studio 2010 Beta 2.

If you evaluated Visual Studio 2010 Beta 1, you will be happy to hear that no reformatting is required. Additionally, simple path exists to appraise Beta 2 on the same machine. It involves uninstalling your Visual Studio 2010 Beta1 product and then installing one of the Visual Studio 2010 Beta2 offerings.  Please refer to the following blog post for more details.

As part of our Beta 2 testing, we did test the uninstall of Beta 2 to ensure that the Beta 2 could be uninstalled sufficiently to the end that you could uninstall Beta 2 and install RTM with as seamless UX as possible.

You can read the entire thread here.

So go for it! The download of VS2010 Beta 2 will be available to the public on October 21, 2009, with final RTM to be released on March 22, 2010.

Crash Course to Web Development for FoxPro and Web Connection Developers

I just received an e-mail announcement for what looks like an awesome jump start class for Visual FoxPro developers to get their feet wet in the .Net Web development arena. Super-smart web maven Rick Strahl plans to give you 3 days of quick start how-to teaching on the .Net / Web development train.

You can attend live or watch on GoToMeeting. It’s a 3-day class, November 5, 2009 through November 7, 2009.

Here is a link with all the details:

(P.S. – Does anyone want to sponsor me?)