Have 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… :)
First 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”.
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
3: public static IEnumerable<T> Randomize<T>(this IEnumerable<T> enumerable)
5: var r = new Random();
6: return enumerable.OrderBy(x => r.Next()).ToList();
So 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!
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…
Add 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…
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!
What’s the Holy Grail of Application Profiling? Well, it should be…
So… How do we create the Holy Grail of Application Profiling? We use a very cool technology called AOP ( Aspect Oriented Programming ).
AOP complements Object-Oriented Programming (OOP), giving us even more tools that increase modularity and allowing us to elegantly handle “crosscutting concerns” such as logging, exception handling and other common challenges. One of the most popular AOP tools is PostSharp (http://www.sharpcrafters.com/), which allows us to “IL Weave” our profiling code ( the act of adding code as IL instructions into the assemblies at compile time )! Sweet…
So let’s start at the end – what do we get?
We’ll if you start a brand new “MVC 4 Web Project” in Visual Studio – by adding a couple references and ONE line in your AssemblyInfo.cs file…
[assembly: ProfileAspect(AttributeTargetTypes = "ProfileWeb.Controllers.*")]
We can start logging to SQL and, for example, easily extract really rich data on pages hit and their load times such as…
Nice… Not bad for ONE line of code… Intrigued? I know you would be… :)
Download the Profiler Code (44 MB)
Bachelor's in Mechanical & Aerospace Engineering, Master’s Degree in Computer Science & Applied Mathematics with a focus on Artificial Intelligence and Computer Cognition.
Enter your email address to subscribe to this blog and receive notifications of new posts by email.
Join 786 other subscribers