Lance Larsen - .NET Architect Madison Wisconsin

All posts in .NET

large_jquery_logoIn my blog “NuGet – Limiting updates for jQuery and Bootstrap” – we looked at continuing to utilize NuGet to keep us up to date on the latest jQuery 1.x version ( even though 2.x is out ).  Reason being that we needed to stick with 1.x because we are still supporting IE 7/8. :(

So… jQuery 1.x and 2.x have feature parity – but you want to use jQuery 2.x when you can, simply because it’s a smaller download as they were able to take out a lot of IE7/8 “fixes”.  Note, jQuery’s post on conditionally include one or the other based on the version of the browser.

But we also like to use the cool MVC bundling and minification feature – can we have the best of all worlds? Oh ya… :)

Read more

CFNeed to setup a “Created” DateTime field in your SQL table that is auto-computed to add the current date and time that the field is inserted into the database? 

If you’re doing so in SQL – in the “Default Value or Binding” you just need to enter GETDATE() or GETUTCDATE() – insert a record, and you’ll see the date/time value automatically added to that column.  “…that was easy…”

But how to do the same thing when using Entity Framework Code First?

No big deal here either – as long as you add the couple lines of code described here…

Read more

imageimageI love to say “…if it’s not in NuGet, it’s DEAD to me…” – and for the most part that is very true. :)

If you don’t know what NuGet is – you MUST – look at Introducing NuGet: the smart way to bring bits into your projects

However, as I and many developers need to continue to support IE7/8 – we can’t update to the latest versions of jQuery or Bootstrap, as jQuery version 2.+ and Bootstrap version 3.+ no longer support IE7/8…

Also considering jQuery is continuing to update their 1.* version with updates – how do we handle that and continue to use NuGet inside of Visual Studio to show us when there are updates that we DO want to install?

Here’s the solution!

In your “packages.config” file – add the allowedVersions tag to both as follows ( noting that the right parenthesis looks strange, but it is correct – it means ‘less than version X’ )…

<package id="jQuery" version="1.10.2" allowedVersions="[1.9.1,2)" targetFramework="net45" />


<package id="bootstrap" version="2.3.1" allowedVersions="[2.3.1,3)" targetFramework="net45" />

imageNow when you open up NuGet ( noting that you need to close and reopen VS and the project ) – you see the following!

For this, we were on Bootstrap 2.3.1 – and only saw an upgrade to 3.0 – but now we see we can upgrade to the latest 2.3.2!  And we’re currently at the latest jQuery 1.10.2 – so correctly don’t see an update – but we will when one comes out. :)

I love NuGet… :)

So we will pick up from where we left off in “Custom Configuration Pattern 101” – we are looking to elegantly consume complex configuration settings in App.config and Web.config files – in this case we are looking to step up our game and consume the following…

<CertificateSection authenticationMode="Test">
    <add certificateType="cipherCertificate"
    <add certificateType="serviceCertificate"

We will continue our “CustomConfigurationPattern” console application!

Read more

So if you’ve even needed to store custom data in your App.config or Web.config files before – you’ve probably used the ConfigurationManager.AppSettings for name/value pair data – but what if you have more complex settings that you want to store in the config files?

Such as…

<Certificate findValue="7776F8E373CF19EEEC88293031893B8D985792F8" 

What do we do?  We extend .NET’s System.Configuration.ConfigurationSection of course… :)

Read more

dotnetConfHave the dotnetConf live stream up and running while working… Totally free – online 2-day developer conference.  It looks to be a fantastic line up of talks both today and tomorrow!  Join dotnetConf

Last we saw our intrepid developers they were creating their first “Code First + Migrations” 101 project – today we join them facing the dreaded “we want to do code first, but already have an existing database!!!”.  Can they survive? Absolutely, and here’s how… :)

imageFirst we need to add “Entity Framework Power Tools” – click on “Tools” –> “Extensions and Updates…” –> select “Online” and then search for “Entity Framework Power Tools” –> “Install” and we have added some great new EF Power Tools features!

Now that we have EF Power Tools installed – if we right-click on our project we should see “Entity Framework” as one of the options – and off of that we have “Reverse Engineer Code First” and “Customize Reverse Engineer Templates”.

Read more

Had the need to randomly shuffle an IEnumerable collection that I was bringing back through Entity Framework – so did some searching and came across a couple solutions – some that were completely crazy – but took the best ideas, combined them and then wrapped the final solution into an extension method that I wanted to share. :)  Code is as follows… ( BTW – I love extension methods btw == code & forget )

   1:  public static class IenumerableExtensions
   2:  {
   3:      public static IEnumerable<T> Randomize<T>(this IEnumerable<T> enumerable)
   4:      {
   5:          var r = new Random();
   6:          return enumerable.OrderBy(x => r.Next()).ToList();
   7:      }
   8:  }

imageSo just by doing the following:

var widgets = db.Widgets.AsEnumerable().Randomize();

Results are randomized, and it Works Beautifully! 

Wrote the following console app – based on my previous Entity Framework “Code First + Migration” 101 blog article – that shows the Randomize Shuffle – so you can Download and try it for yourself!

Good Coding!

Download Code ( ~2.7M )

So for those who haven’t used Microsoft Entity Framework’s “Code First” approach to application development – it’s a very powerful approach that allows us developers to spin up a project with a database back end in record time!  Early on it had the distinct disadvantage that it didn’t allow for changes in your code to migrate to the database – but, as we will see, that feature has also matures and is pretty sweet now… :)

So where to start?

Open Visual Studio, click “New” –> “Project” –> “Console Application” and create your application…

imageAdd Entity Framework to the project.  We’ll do this by using “NuGet”.  Right-Click on the project, select “Manage NuGet Packages…”, enter “EntityFramework” ( usually right at the top ) and click “Install”.  This will add all of the “entity-goodness” to your project!



Right-Click on your project and click “Add” –> “Class…” –> name it “Model.cs” and insert the following code…

Read more

We have a lot of really great events coming up!  Wanted to make sure everyone is aware of how many cool events there are – and how dedicated the people that make these events possible are looking to help you!

Read more