Lance Larsen - .NET Architect Madison Wisconsin

All posts in Entity Framework

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

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