Learning Entity Framework with Asp .NET MVC

Entity framework forms a bulk in learning MVC. There are very less resources for learning Entity Framework.

Books by Julie Lerman are very comprehensive and they look like the authoritative source for reference to Entity Framework.

There is a big tutorial for MVC and Entity framework in asp .net site http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Currently I am going through this tutorial and will blog about this.

You can download the completed code and then quickly browse the source code. But what I noticed is that you cannot straight use code first default initialization as you get foreign key errors when you use the initialization code in the DAL folder.

Using the update-database on NuGet console, you can force the seed script in migrations folders to run which sets the correct database for the downloaded application.

Advertisements

Installing from Local NuGet Respository

A timesaver in using NuGet packages is using the local NuGet cache to install packages to Visual Studio Projects after the package is downloaded first time from NuGet.org.

Everytime we download a NuGet package, it is stored locally in C::\Users[username]\AppData\Local\NuGet\Cache.

Scott Hanselmann has a great post on using this technique to install NuGet packages from local cache once the package is downloaded first time from visual studio.
http://www.hanselman.com/blog/HowToAccessNuGetWhenNuGetorgIsDownOrYoureOnAPlane.aspx

In you case want to create a local repository which you can access from multiple PC’s, you can create a local directory and configure it is a source package in Visual studio. In this you can access all your local repositories from a central location. More about this can be found at https://joshilewis.wordpress.com/2012/01/13/creating-a-local-nuget-cacherepository/

These are great time savers in adding NuGet packages repetedly to project in visual studio. In case you have slow internet connection, this is a godsend.

Learning Querying in Entity Framwork – Tips 1

Learning to write queries in Entity Framework is key to leverage the benefits provided by Entity Framework.

Some utilities which can help us on the journey are:

  • Object Dumper
  • Printing the SQL generated by Entity Framework

Object Dumper

This is the Microsoft utility to dump result of linq queries as an object graph. This is a great utility to see the results from a linq query. We can use object dumper to print out the results of query from Entity Framework to console. In case you want to see the output in a single file, call the method which has the Entity Framework query from a MsTest test case. There is a TestContext property which can capture the console output which can be viewed in the test results pane of MS Test.

Dumping SQL Query generated by Entity Framework

Entity Framework 6 has a neat utility to print the SQL generated by Entity Framework for the query given. Just call ToString on the variable containing the query. This will print the SQL generated to the console.

The above 2 utilities are of great help while learning querying in Entity Framework.

A sample code listing using the above two tips is given below:

public void GetCustomerMini()
        {
            using (var context = new AWEntities())
            {
                var query = from c in context.Customers select new {c.CustomerID, c.FirstName, c.LastName};
                var customer = query.ToList();
                var sql = query.ToString();
                Console.WriteLine("SQL: {0}", sql);
                Console.WriteLine("Count: {0}", customer.Count());
                ObjectDumper.Write(customer);
            }
        }

you can call the above method in MSTest test method and using TextContext property have the console output captured as part of test result.

Linqpad can also make this simpler. But for people who like visual studio the above 2 utilities are of great help

Career Crisis in IT Technology Industry

Why is keeping up to date with fast moving technology so hard?

Most of us from offshore Indian companies work in low end maintenance projects. The Techology we work is old, the work routine and some mission critical systems require a lot of attention for support. Over a period of time when we see the new technologies we feel as if we are from stone age.

Here are few challenges I seen in my career:

  • When we are young we master simple things quickly. We have a lot of energy and are gumtption. We feel as if we know all. But in reality we are only seeing tip of the iceberg
  • As we get older we fall into the other issues politics, slowing career growth, personal and professional setbacks etc. We start feeling things are not greener as we thought
  • We stop learning once we reach a comfort level and then with what we know we don on the role of experts. How ever we have always been standing on top of leaky abstractions
  • We lose touch with programming and our mental skills start slowing down
  • It looks like we have to start learning right from the basics as we have lost many cycles of technology updates
  • When we see other people growing faster than is and young people just mastering a new technology faster we feel lost and let down. “Why me”, “Am I valuable” feelings start to rule our mind
  • Not having strong support network or emotional balance we have breakdown or fall into addictions which lead us farther down the pit
  • We start thinking whether we should make a career change. But what are we to do? What kind of skills we need? How do we make the job change etc etc are issues which grapple us
  • Can we make the same high salary we made previous. If the current job is dead end and makes us sick and we dont feel like working how do we meet our financial committments

The above are some of issues which I feel confronts us at some point in our life.

I have to some extent solved the above issues I have faced in my life. I will be blogging about these so that others can take recouse from what I have done.

Learning Entity Framework

ORM is becoming used increasingly in projects as we want to get rid of SQL development which needs database skills and reliance in DBAs who are already over loaded. Microsoft’s Enity Framework has become a matured product and is the data access methodology suggested by Microsoft.

However Entity Framework has a huge learning curve and there are not much information available on using it. Julie Lerman has some good books and video courses in Pluralsight.

I have not had much success in my attempts for learn Entity Framework partly because I could not put it to use in a real time project.

Now I am tyring to see how to use Entity Framework in some OS projects.

The following are some courses in Pluralsight which are related to Entity Framework and which I plan to go through. I will also write some blog posts on my learning experience.

  1. Entity Framework 4.0 By Example
  2. Entity Framework and Data Models
  3. Designer Supported EDM Customization
  4. Querying the Entity Framework
  5. Entity Framework 4.1 – Code First
  6. Entity Framework 4.1 – DbContext Data Access
  7. Entity Framework Code First Migrations
  8. Data Layer Validation with Entity Framework 4.1+
  9. Entity Framework in the Enterprise
  10. Getting Started with Entity Framework 5
  11. Entity Framework 6: Ninja Edition – What’s New in EF6

You can browse through the courses using following link in Pluralsight

Learning Bootstrap

Just started learning bootstrap using the Pluralsight course Introduction to Bootstrap by K Scott Allen.

I have fallen in love with Bootstrap. CSS was really a pain area for me and I had a lot of failure attempts trying to read CSS from books. Bootstrap has really changed my perception of CSS.

The documentation of Bootstrap is awesome. Neat and clear examples explain how to use the various bootstrap features. The documenation is the main reason I fell Bootstrap has been adopted by so many people.

I will be spending some more time learning and experimenting with bootstrap and will share more about this in future posts.

Unit Testing with NUnit

Unit testing using testing frameworks is a very important activity for any developer. With the nature and types of testing ever under the rise, it becomes important to use automation where ever possible.

Even though there are lot of unit testing frameworks, the simplest and the easiest to user for many scenarios is Nunit.

Though Microsoft has its own MSTest framework, it falls short in terms of the API and the learning materials tutorials for using it. Surprisingly this is one area which is under documented in Microsoft space.

Though there are lots of tutorials to get started with NUnit, I have found the following very good to start with:

Pluralsight Course: Introduction to .NET Testing with NUnit

For samples the best I have seen is the nunit-csharp-samples.

I will also share some of the good scripts and samples I see in forthcoming blogs.