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

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

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