DCSIMG
Execute T-SQL Statements in Entity Framework 4 - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Execute T-SQL Statements in Entity Framework 4

Execute T-SQL Statements in Entity Framework 4

In this post I’m Execute T-SQL Statements in Entity Framework 4
going to show a
new Entity Framework 4
feature that enable us
to execute T-SQL
from the ObjectContext
directly to the database.

Executing T-SQL Statements from ObjectContext

One of the new improvements in EF4 is the ability to execute
T-SQL store queries and commands. This ability enable the EF
developer to execute T-SQL directly against the database. This ability
should only be used in cases that Entity Framework doesn’t support
something that you need which can be a very complicated query for
example. In order to use this feature the ObjectContext supply two new
methods: ExecuteStoreQuery<T> and ExecuteStoreCommand.

Using ExecuteStoreQuery<T> Method

The ExecuteStoreQuery<T> method should be used to query
data and return it in the shape of the given generic parameter.
The method will only work if T has default constructor and also
there is a one to one match between the returned column names
and the class property names. This imply that you can use any class
that you want which confirm to the restrictions as the T generic
parameter and not only EF generated entities. If the classes are
entities you can provide the EntitySet name and a MergeOption
in order to connect the entity to the ObjectContext.
You use the method like in the following example:

class Program
{
    static void Main(string[] args)
    {
        using (var context = new EntityFrameworkExampleEntities())
        {
            var query = context.ExecuteStoreQuery<Company>("SELECT * FROM Companies");
            foreach (var company in query)
            {
                Console.WriteLine(company.CompanyName);
            }
        }
        Console.ReadLine();
    }
}

This is a very simple query that only show the concept of how to use
the method. In such queries I won’t bother to use ExecuteStoreQuery<T>
since I can use LINQ to Entities instead.

Using ExecuteStoreCommand Method

The ExecuteStoreCommand method should be used to send a command
to the database such as update, delete or insert. The return value is
the number of rows that were affected by the command you sent.
The following is a very simple delete command:
class Program
{
    static void Main(string[] args)
    {
        using (var context = new EntityFrameworkExampleEntities())
        {
            var count =
                context.ExecuteStoreCommand(@"DELETE FROM Companies 
                                              WHERE [CompanyID]=4");
            Console.WriteLine(count);
        }
        Console.ReadLine();
    }
}

As in the previous example this is only a simple example.

Summary

Lets sum up, there are times that you hit a wall when you use EF.
It can occur in very complicated commands or queries that you need
in your application but LINQ to Entities can’t help you with them. In
such situations ExecuteStoreQuery<T> and ExecuteStoreCommand
can come to your help.

DotNetKicks Image

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# November 25, 2009 9:49 AM

Gulino Barnes said:

Great Information! Thanks for sharing!

# November 25, 2009 1:22 PM

Twitter Trackbacks for Execute T-SQL Statements in Entity Framework 4 - Gil Fink on .Net [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Execute T-SQL Statements in Entity Framework 4 - Gil Fink on .Net         [microsoft.co.il]        on Topsy.com

# November 26, 2009 9:55 PM

Gil Fink on .Net said:

EF Feature CTP5: Raw SQL Query/Command Support One of the new features that EF feature CTP5 supplies

# December 8, 2010 4:19 PM

Executing entity | Bibliyo said:

Pingback from  Executing entity | Bibliyo

# March 19, 2013 11:21 AM