Wriju's BLOG

.NET, Cloud and everything

ADO.NET EF and Repository Pattern

This is a fantastic blog series by Julie and who don’t know Julie??? Repository Pattern this is ideal for testing and mocking. Julie is one of the wizards of EF has written a series, Agile Entity Framework 4 Repository: Part 1- Model and POCO Classes http://thedatafarm.com/data-access/agile-entity-framework-4-repository-part-1-model-and-poco-classes/ Agile Entity Framework 4 Repository: Part 2- The Repository… Read more

Entity Framework CodeFirst : Simple Migrations

When I change my model from let’s say //Employee Entity public class Employee { public int Id { get; set; } public string FullName { get; set; } } to //Employee Entity public class Employee { public int Id { get; set; } public string FullName { get; set; } public string Email { get;… Read more

Entity Framework CodeFirst : Where is my database

Sometime back I was trying out on CodeFirst and I suddenly realized that the generated database by CodeFirst is missing. I had to see in SSMS what exactly got generated. So I did a trick to retrieve the database information //Employee Entity public class Employee { public int Id { get; set; } public string… Read more

Resources on ADO.NET Entity Framework 6

New Features in EF6 These are some of the more notable new features in EF6. For a complete list of changes see Version History. Async Query & Save (EF Designer & Code First)Asynchronous query and save using the async and await keywords. Connection Resiliency / Retry Logic (EF Designer & Code First)Automatically retry any commands… Read more

Repository Pattern with ADO.NET Entity Framework Hack

Let’s build ADO.NET Entity Framework library using Repository Pattern. There are a many ways but this is probably the easiest hack.  First build a blank solution Then add a class library called “DAL” and add a class called “Employee” namespace DAL { public class Employee { public int Id { get; set; } public string… Read more

ADO.NET Entity Framework : Executing TSQL and DbContext under same Transaction

There might be a requirement that we need to run a RAW SQL statement and DbContext update under same connection and would like to have the Transaction in place. Let’s see how we can achieve this easily SQL Table CREATE TABLE [dbo].[Emp]( [Id] [int] IDENTITY(1,1) NOT NULL, [FirstName] [varchar](50) NOT NULL, [LastName] [varchar](50) NULL, CONSTRAINT… Read more

Using Repository Pattern in Entity Framework

One of the most common pattern is followed in the world of Entity Framework is “Repository Pattern”. Since this is something which is heavily used and being practiced, I am not going to talk about the core pattern. Rather, try to show how one can implement it. Objectives As mentioned in http://msdn.microsoft.com/en-us/library/ff649690.aspx You want to… Read more

Entity Framework CodeFirst Calling Find does Top 2

While using ADO.NET Entity Framework’s CodeFirst and we call Find() method against the PrimaryKey field we get one record. But surprisingly if we dig deeper we will be able to see that it is calling Top 2 which means actually getting two rows. Why is it so? Because just to track if there are more… Read more

Entity Framework 5 and 6 Roadmap

Entity Framework 5 The Entity Framework team is in the final stages of completing the EF 5 release. Entity Framework 5 RC is currently available on NuGet and introduces the following new features: ·         Enum support allows you to have enum properties in your entity classes. This new feature is available for Model, Database and… Read more

Channel 9 Video on Entity Framework 4.1

Published the video on ADO.NET Entity Framework 4.1 Code First Development video. This covers quickly the basic features if this amazing product. Check it out at https://channel9.msdn.com/posts/EF41CodeFirst The code used for the demo is using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; using System.ComponentModel.DataAnnotations; namespace EF41CodeFirst_BDotnet { //Dept Class public class Dept {… Read more

Code First EF 4.1 : Table per Hierarchy

Concept of Table Per Hierarchy (TPH) is to implement polymorphism and to de-normalize the relational database via inheritance. The entire hierarchy is mapped to a single database table holding all the properties of all the classes. The database then would create a column called “[Discriminator]” to hold the specific type. In Code First this is… Read more

Using ADO.NET EF 4.1 Code First with existing Database

Most of the time questions comes to our mind about EF 4.1 Code First whether or not can we use it with already available database? Simple answer is YES. I have very few personal view on it. I have seen in most of my application development scenario, database gets designed and created before and then… Read more

Code First EF 4.1 : Querying Many to Many Relationship

In continuation to my previous post on how to create one to many, let’s see how can we query it. We will use the same code base and query it Option 1 When we want to query it normally like below var emps = ctx.Emps .Where(e => e.EmpId == 1) .SelectMany(e => e.Projects, (em, proj)… Read more

Code First EF 4.1 : Building Many to Many Relationship

Since we do not have any designer question might arise how can we create Many to Many relationship in Code First 4.1. Here it is public class Emp { public Emp() { Projects = new HashSet<Project>(); } public int EmpId { get; set; } public string EmpName { get; set; } public ICollection<Project> Projects {… Read more

CodeFirst EF 4.1 : Changing Database Table and Column name

At times we need to control the Table and Column name of our generated database or have different Entity/Property name of code than actual database in EF 4.1 Code First. There are two ways we can do it. Using Annotations using System.ComponentModel.DataAnnotations; //Changing database Table name to Employee [Table("Employee")] public class Emp { //Changing database… Read more

Code First EF 4.1 : Missing “ADO.NET DbContext Generator” VS Template

I have seen people coming back to me asking questions about the template visible in Visual Studio 2010 as “ADO.NET DbContext Generator”. This template allows you to create the POCO classes and the context for CodeFirst from already existing edmx file. The template in Visual Studio looks like But missing. But why? At times if… Read more

Code First 4.1 : Using Stored Procedure to Insert Data

Code First in Entity Framework does not support Stored Procedure by default. As there is no designer we cannot even map our stored procs to the entity. There are a many scenario we have seen where we are bound to use stored procedure for any database modifications (insert/update/delete). Here is how we can use stored… Read more

EF 4.1 Code First Validation

When we either use the Fluent API or the attribute to put restrictions to our properties and while adding if we violet that, error would occur. Code First provides us set of classes to capture them Code First Model public class Department {     [Key]     public int DeptId { get; set; }     public… Read more

ADO.NET Entity Framework Code First 4.1 – Online

April 20, 2011 at 11 AM Pacific Time. Please register at https://training.partner.microsoft.com/learning/app/management/LMS_ActDetails.aspx?UserMode=0&ActivityId=731858 Learn what is new in ADO.NET Entity Framework Code First 4.1 ADO.NET Entity Framework comes with a new update to make things simpler while accessing data. ·         Background – no edmx, no visual model ·         Assemblies ·         Create and Load Database ·         Data… Read more

EF 4.1 Code First – Running SQL Queries

ADO.NET Entity Framework’s Code First 4.1 allows us to execute SQL queries directly. You may need to get one column output or quickly update a table. One thing we need to be careful about running these raw queries is that you need to be double sure about the validity of these… If you are viewing… Read more

EF Code First 4.1 Lazy Loading

EF Code First supports Lazy Loading as Model (edmx). You need to define the navigation properties to set the relationship to load the query. So if we try to implement the below code 1: public class Department 2: { 3: [Key] //To make it Primary Key and Identity 4: public int DeptId { get; set;… Read more

ADO.NET Entity Framework Code First Development

I liked LINQ to SQL for its clean approach of defining only code to be able to access the database. ADO.NET Entity Framework comes with a much more richer experience of you being the fine grained control over the code. In this current version we have Model First and Database First approaches, now we have… Read more

TechEd 2011 India

Thanks everyone. It was pleasure talking to all of you in India TechEd 2011 on “Choosing Right Data Access Methodologies”. It was a jam packed session and a lot of people were standing or sitting on the stage. I loved talking. Huge!!! I have been getting emails to share resources. Let me tell you one… Read more

TechEd 2011 India – Special

I have been following TechEds since 2005. This time India TechEd 2011 is very special to me. I am delivering session called Data Access Methodologies: When to choose what (ADO.NET, Entity Framework, LINQ, WCF Data Services) and the official abstract says, Often an Application requires to access database. A lot of evolving data access technologies… Read more

ADO.NET Entity Framework : Adding User Defined Function in Storage Model

Sometimes you may want to use same logic again and again. Let’s suppose you do not have access to your database which allows you to write function or stored proc. Never mind, EF allows you to define the logic at Model and you should be able to experience the same. You want to search an… Read more

ADO.NET Entity Framework Performance Tips

Over a period of time I have been working with ADO.NET Entity Framework and I am flattered with its amazing usability. I am a big fan of EF and especially with EF4 release a lot more avenues are open. I love to talk about this technology in any forum and given a chance I would… Read more

ADO.NET Entity Framework Transaction

Any data-driven application needs Transaction and to ensure that the unit of work is either together successful or fail. Transaction plays a very important role. Assume that we are working with SQL Server. Now SQL Server has it’s own Transaction mechanism which could be used in Stored Procedures. However, not every time a Transaction is… Read more

ADO.NET Entity Framework POCO Resources

Some of the available finest resources on POCO MSDN Articles Requirements for Creating POCO Proxies Loading Related POCO Entities Tracking Changes in POCO Entities How to: Define POCO Entities How to: Define a Custom Object Context How to: Customize Modeling and Mapping Files to Work with Custom Objects How to: Define a Custom Object Context… Read more

Entity Framework finding the differences between production database and model’s schema

We develop our ADO.NET EF model from the development environment db and then when finally run it to the production we might find that DBA might have added few rules or renamed the database columns. Then our application would start throwing errors. One easy way to get it rectified is to run query against each… Read more

EF4 : How to enable Model without code

When we need to work with an Entity Framework model and use POCO. We need to get rid of the designer generated code. But how? There are few ways by which we can achieve it. Option 1 I personally like this approach. You need to open the Model designer and click F4. The change the… Read more

EF4 : Complex Type

Is to simplify your life which might seem too complex :). Let’s suppose you have a table like, Now you might want to club all address related field and make one single representation to it. Once you have created the model in your Visual Studio 2010, you may just need to select all the properties… Read more

EF4: What’s in it there in Foreign Key

There are a lot of buzz around the Foreign Key support in ADO.NET Entity Framework v4. Question often comes to our mind that what is so special here? While discussing with my fellow Consultant Vijay, he gave me a nice example. Let me share here, Let’s assume that you have two tables Dept and EmpDept…. Read more

EF4 : Use MergeOption.NoTracking for better query performance

If you are just trying to fetch the data and has no intention to update it. Then you should be using the MergeOption.NoTracking to ensure that the ObjectStateManager does not store the required information for update and delete. Hence your query would be more quick in nature. using (NorthwindEntities ctx = new NorthwindEntities()) {     ctx.Customers.MergeOption… Read more

ADO.NET Entity Framework 4.0: Making it fast through Compiled Query

If we are using similar query frequently, we can increase it’s performance by Compiling it through CompiledQuery. It’s always recommended to use CompiledQuery if you happen to see the query is getting executed many times. Let’s take an example, in Northwind database if you are getting Customer based on City. You may follow the below… Read more

ADO.NET Entity Framework 4.0: Simple N-Tier with Self-Tracking Entities and WCF

ADO.NET Entity Framework 4.0 allows us to easily create N-Tier Application with the help of Self-Tracking Entities. This means now we do not have to worry about merging and checking values in more disconnected scenario. Here is how we can do it in very simple example to start with. Create Model Layer ·         Create a… Read more

ADO.NET Entity Framework 4.0 : Loading Data in 4 ways

  We are working with Relational Database and using ADO.NET Entity Framework 4.0, it is inevitable that we need to load data. EF4 has improved Lazy Loading feature as compared to its previous version. We can also combine both Lazy and Eager loading.   Based on the amount of data we bring in and type… Read more

ADO.NET Entity Framework 4.0 : DataBinding with Windows Presentation Foundation (WPF)

This post is to give an idea on how easily we can bind and create master details display in WPF.   Here I am using Northwind sample database and using Category and Products tables. These two tables are connected with each other through CategoryId. We will create a model using these two tables. Please ensure… Read more

ADO.NET Entity Framework 4.0 : DataBinding with Windows Forms

This post is to give an idea on how easily we can bind and create master details display in Windows Forms Application.   Here I am using Northwind sample database and using Category and Products tables. These two tables are connected with each other through CategoryId. We will create a model using these two tables…. Read more

ADO.NET Entity Framework 4.0 : DataBinding with ASP.NET

This post is to give an idea on how easily we can bind and create master details display in ASP.NET. Here I am using Northwind Sample database and using Category and Products tables. These two tables are connected with each other through CategoryId. We will create a model using these two tables. Please ensure that… Read more

ADO.NET Entity Framework 4.0 : POCO – The Code Only Approach

POCO in ADO.NET Entity Framework 4.0 has been discussed in many places. Here I am not going to talk about it. However, I created a small sample to demonstrate the capability of POCO in much more cleaner “code-only” approach. <<Apology for this blog post issue>>. There were some images, due to which it was not… Read more

ADO.NET Entity Framework v2 : ExecuteStoreQuery

There is now an option in ADO.Net Entity Framework v2 for us to execute store query,   IEnumerable<Customer> custs =  ctx.ExecuteStoreQuery<Customer>(“SELECT * FROM Customers”, null);   foreach (var c in custs) {     Console.WriteLine(c.CustomerID);     }   Namoskar!!!  … Read more

ADO.NET Entity Framework : Editing a detached Object

In Layered scenario, you might need to pass an object to another method while updating. Carrying open Context is not good however you may try the below approach by using “CreateEntityKey”. But this will not work if you have configured your edmx with Stored Procedure, reason behind that is static void Main(string[] args) {    … Read more

Data Services (aka Astoria) : CRUD (Select / Insert / Update / Delete )

Creating Astoria is very easy and many of you might have tried that out. What important for us is to work with that data. If you are developing Visual Studio Solution then things are quite obvious as mentioned below, Either you will have Astoria or you will create that service. I am not going in… Read more

ADO.NET Entity Framework and Data Services Case Study

Story of Misys Healthcare Systems is very interesting. They have increased 60% of their development process while migrating their apps to ASP.NET Ajax using Entity Framework and Data Services.   Full story at http://www.microsoft.com/casestudies/casestudy.aspx?casestudyid=4000002427   Namoskar!!!… Read more

ADO.NET Entity Framework vs LINQ to SQL

  I have been getting a continuous request on what we should be using? LINQ to SQL or ADO.NET Entity Framework? I have posted few articles on that, please feel free to visit   Choosing between ADO.NET Entity Framework and LINQ to SQL Choosing between LINQ to SQL and Entity Framework   ADO.NET Entity Framework… Read more

ADO.NET Entity Framework Resources

Compiled Resources http://www.renaissance.co.il/downloads/Entity%20Framework%20Essential%20Resources.pdf   Articles   Next-Generation Data Access: Making the Conceptual Level Real http://msdn.microsoft.com/en-us/library/aa730866(VS.80).aspx   The ADO.NET Entity Framework Overview http://msdn.microsoft.com/en-us/library/aa697427(VS.80).aspx   ADO.NET Tech Preview: Entity Data Model http://msdn.microsoft.com/en-us/library/aa697428(VS.80).aspx   ADO.NET Entity Framework Overview http://blogs.msdn.com/adonet/archive/2008/03/27/ado-net-entity-framework-performance-comparison.aspx   Channel9 Videos   ADO.NET Entity Framework on Channel 9 Pablo Castro, Mike Pizzo, and Britt Johnston http://blogs.msdn.com/adonet/archive/2006/07/19/671805.aspx  … Read more

Choosing between ADO.NET Entity Framework and LINQ to SQL

As part of my day to job I come across a very common question from the developer community that one should be using LINQ to SQL (L2S) or ADO.NET Entity Framework (EF).   Earlier I have posted a blog containing the features http://blogs.msdn.com/wriju/archive/2009/01/05/choosing-between-linq-to-sql-and-entity-framework.aspx.    To me I feel very excited about EF as it has got… Read more

ADO.NET Entity Framework : Working with Stored Procedure

Here we go, I generally get questions from developers that if they will have to scrub the existing powerful stored procedure they have written by investing time and money. The answer to that is “NO”. You must enjoy using it as it has its own power.   So it is very simple in ADO.NET Entity… Read more

ADO.NET Entity Framework : Paging EntitySQL

Paging in EntitySQL is really easy to achieve. You have got two important sub-clauses (or query operators) are available, SKIP and LIMIT.   So your paging EntitySQL query would ideally look like, “SELECT VALUE e FROM NorthwindEntities.Customers AS e ORDER BY e.CompanyName SKIP 10 LIMIT 10 “;   The most important part is that you… Read more

ADO.NET Entity Framework : Using EntityClient and EntitySQL

Using Entity Framework with LINQ is simple and fun stuff. But if you are concerned about the performance and comfortable writing ADO.NET like code then EntityClient is for you. It resides under System.Data.EntityClient namespace. If you are using it you also need to use the EntitySQL (not T-SQL) so the query will be little different…. Read more

ADO.NET Data Services Resources

Help yourself by exploring below resources on ADO.NET Data Services (aka "Project Astoria"). Data Platform Developer Center – Data Services page Data services conceptual documentation on MSDN Data services API reference documentation on MSDN Data services team blog Data services online forum (a place to ask us your data services related questions) Whitepaper: Using ADO.NET… Read more

LINQ to Entity : Supported and Unsupported Methods

There is a correction for this Post, so do I have changed the title. Thanks to Chris Love who pointed me the faults with earlier post, I got few question on some of the popular paging methods of LINQ which does not work on LINQ to Entity. This is by design and quite logical. In… Read more

ADO.NET Entity: LINQ to Entity with Relationship

Relationship and getting data from Entity Framework using Northwind database Simple Query   using (NorthwindEntities ctx = new NorthwindEntities()) {     var query = from o in ctx.Orders                 select o.OrderID;       foreach (var k in query)     {         Console.WriteLine(k);     } }   Now if you want to filter this with the… Read more

ADO.NET Entity: Insert Update and Delete with Relationship

Few days back I had written an article on Insert/Update/Delete for simple standalone tables at ADO.NET Entity: Insert Update and Delete. Now after that many of you had requested me to put article on how it works with relationship.   Here I will use a database created by me. There will be two tables connected… Read more

ADO.NET Entity: Insert Update and Delete

For small blogs, it requires more references and explanation, which sometimes are redundant. So I thought of writing single blog which is combination of topics more or less related to one thing.   I am exploring ADO.NET Entity Framework and have been trying things out there. Here I am planning to demonstrate how to do… Read more

LINQ to Entity : Querying data using ADO.NET Entity Framework

ADO.NET Entity Framework have evolved since we have released it first time. Now with the Visual Studio 2008 if you install the .NET Framework 3.5 SP1 and Visual Studio 2008 SP1 you will get the fully functional version of ADO.NET Entity Framework. This is post Beta 3 release. To create a .edmx file you have… Read more

ADO.NET Entity Framework 3.5 Beta 3 Released today

Beta 3 of the ADO.NET Entity Framework, and a preview of ASP.NET 3.5 Extensions. Designed to make it even easier for developers to build data-centric applications and services regardless of the underlying data source, the ADO.NET Entity Framework is already gaining significant support from multiple database vendors and third-party providers, including Core Lab, Data Direct… Read more