Zeeshan Hirani

  • Lazy Loading Related Properties

    Lot of times in our application we do not perform eager loading on related entities because we don’t need to display them on the UI unless some action occurs. So we fetch those entities lazily only when we need them. However have you ever felt the need to lazily fetch several related entities? Suppose we have a model consisting officers who gave tickets to Drivers as shown below.

  • Removing entity from a Related Collection

    I have seen this misunderstanding starting to popup more often on how to delete an entity from a related collection. Suppose we have the following entity data model where a Person has many Phones identified by 1-to-many association as shown.

  • Setting Entity Key for a view

    When you import a database view using Entity Data Model wizard,you will get an entity that has the same number of columns and data type as the view does.Suppose I have the following view in my database.

  • Search for an entity in your Entity Data Model

    Last week i discovered a feature of Entity Data Model which i have not seen extensively being promoted. I accidentally run into it because of the size of my model. If you have a large model and want to search for an entity, it becomes tedious to zoom in and out and scroll up and down to find your entity. In Entity Framework 4, on the model browser there is a new search option that allows you to search for any artifact on your model, whether it be a property, association, column, entity. Below are some ways to search for any artifact on the model.

  • Watch out for let operator in linq to entities

    LINQ has a really cool operator call Let that lets you declare variables inside the query. The benefit i find in using let is, it makes my query much easier to read because using Let keyword i can create a variable and assign it a calculated value. Then later in my query i can test my value against that variable. In LINQ to entities if you are using let keyword, you want to be aware of how the sql is generated. There are cases where let keyword introduces extra or repetitive joins that are un-needed. Let’s see an example of where let introduces extra join when the linq query is translated. In the figure below I have a SalesRep entity that has an optional 0..1 association with RepSalesStatus. RepSalesStatus table in the database contains the totalsales a given rep has done. Since the association is optional there could be reps in our database that have not done any sales as yet.

  • Small SQL improvement in Table Per Hierarchy

    Well the title of my post is somewhat disguising the actual point which I wanted to communicate. There is basically two important things that i wanted to talk about in this posting. First a slight improvement in T-SQL generation for Table Per Hierarchy from version 1 of EF. Secondly an existing smart T-SQL generation for Table Per Hierarchy which had always been there but i never noticed it. Let’s dwell into some of these features by starting with an existing model that i have already created.

  • Contains is smart enough to check for null

    Starting with Entity Framework 4.0 you can use Contains clause in your linq query to check if the column value matches any values in a given list. Suppose we have a model with Product and Category as shown below.

  • How to share common fields between two entities that map to different tables

    The title is a mouthful so let me explain what we are trying to solve. Suppose we have two tables in the database that have common fields. When we import the tables into Entity Data Model, we want to create a base entity to contain the common fields and let other entities derive from it. Since the base entity does not map to any table in particular in our database, we want to make base entity abstract. If you have reached this far and understood what the problem is, then follow along to know to solve it!

  • How to perform left join in linq to Entities

    To perform left outer join in a linq query, you would normally use DefaultIfEmpty operator. DefaultIfEmpty opreator basically gives you a default instance of an object when there is no related record found. In version 1 of Entity Framework, DefaultIfEmpty was not supported, therefore you had to apply different techniques to get around this problem. I am very happy to see that DefaultIfempty is finally supported in Entity Framework version 4. Let’s walk through an example of how to use DefaultIfEmpty operator to get a left join.