Zeeshan Hirani

  • Configuring Existing DataBase with Many-to-Many using Code First

    With CTP5 of Code First, you can easily map existing database schema to classes in your project.  It allows you to configure what class and property gets mapped to what table and column in the database. Code First gives you two options to configure the mapping. You can either use DataAnnotation such as attributes or use the fluent api to explicitly control every single detail about the mapping. In the existing database schema, I had a many-to-many table and I was wondering what would be the cleanest way to map that to an association in my code first. Let’s see an example.

  • Builder Methods to create dynamic esql queries

    In the current project, I am working on,  I make use of esql heavily. One of the reason is because I am still using EF v1 which has limited support for linq queries. The second reason is, if you have dynamic queries which are build on what user selects from the search criteria, esql makes the job much easier. This is why Entity Framework supports the concepts of using builder methods.

  • Filtering and Ordering Include

    Lot of times in your application, you have a requirement to eagerly load related collection but you want to load the collection partially and also have the data sorted when before it is materialized at the client layer. Currently in EF, there is no direct way to filter and sort and Include but there is a workaround. In this blog post, I will show you how you can use anonymous type to paritally load a collection in the correct order you desire.

  • Binding Stored Procedure Result from Entity Framework to ObjectDataSource Control

    Not too long ago someone asked me how can they bind data returned from stored procedure to a gridview control. There are several ways to achieve that. You can either use EntityDataSource or ObjectDataSource Control. I find it easier to use ObjectDataSource control because it allows me to encapsulate logic inside my classes. The basic steps are as follows

  • Inserting,Updating and Deleting entity using Stored Procedures

    If you have used entity framework with stored procedure, you would realize it has a great designer and runtime support. Yes there are few gotchas and loose ends that needs to be fixed but it does the job 99% of the time. In this blog, I will show you how to use stored procedures to perform crud operations on a single entity.

  • Upgrading Independent Association to Foreign Key Association

    In EF when you want to define a relationship between two entities, you typically use an association. An association can be many-to-many, 1.. 0..1, many-to-1 etc. Typically you would have tables in the database which are related to each other using foreign key constraints. When you import those tables in Entity Framework, EF would create an association.

  • Thanks EF4. Now i can put my orderby clause anywhere

    Well most folks wouldn’t have a clue as to what the title of my posting says. This is a problem that stems from EF v1  but is now solved for the most part. in EF4.0. In version 1 of EF, it was recommended that you put your order by operation as the last operation in the query. If you do not put order by as the last operation in the query query, order by would be ignored in some cases.

  • Watch out for StartsWith when upgrading from .net 3.5 to 4.0

    Currently I am working on .net 3.5 project so i am having to live with Entity Framework. v1. Anyways i was working on a linq query that was making use of StartsWith operator. For some reason based on which page you are coming from the value passed in to the StartWith was sometimes empty. To be more clear let’s look at a sample code to understand how my scenario looked like.

  • Which one is faster SingleOrDefault or FirstOrDefault

    Generally, I don't see too much performance problem whether you use Single Or First. However if you have a table with lots of columns like 300 you might notice slight improvement if you use FirstOrDefault. Before we go deeper into the performance difference let’s understand how they are semantically different.