Showing posts with label Entity Framework. Show all posts
Showing posts with label Entity Framework. Show all posts

Wednesday, February 02, 2011

Entity Framework - Stored Procedures with Scalar Output Values

Yesterday was the time I needed to execute a Stored Procedure with EF4. My immediate thoughts were "no problem", I'll just pull in the Stored Procedure with the EF designer, which will create the FunctionImport and then I'll Google with Bing for the syntax to make the call and away we go...

The sproc in question accepts a single input parameter and returns two output scalar values and to my frustration I soon discovered that EF currently supports sproc calls that return an Entity Type, but not sproc that return scalar values.

So I again Googled with Bing to find a solution or workaround and discovered a few suggestions. A few posts such as this one suggest altering the sproc to return a simple entity e.g. SELECT @Id = SCOPE_IDENTITY() AS Id; Others suggest using the EFExtensions or upgrading to EF4 which apparently supports this directly.

The sproc is in a legacy database which I could not alter and the EFExtensions or upgrading to EF4 seemed like overkill to make a single simple sproc call.

I eventually settled on making the call directly as follows:
EntityConnection entityConnection = (EntityConnection)context.Connection;
DbConnection storeConnection = entityConnection.StoreConnection;
DbCommand command = storeConnection.CreateCommand();
command.CommandText = "MY_STORED_PROCEDURE";
command.CommandType = CommandType.StoredProcedure;
SqlParameter idParam = new SqlParameter("p_id", SqlDbType.Float);
idParam.Value = myInputValue;
SqlParameter treeParam = new SqlParameter("p_tree", SqlDbType.VarChar, -1);
treeParam.Direction = ParameterDirection.Output;
SqlParameter privateParam = new SqlParameter("p_private", SqlDbType.VarChar, -1);
privateParam.Direction = ParameterDirection.Output;
command.Parameters.Add(idParam);
command.Parameters.Add(treeParam);
command.Parameters.Add(privateParam);
DbDataReader reader = command.ExecuteReader();
Console.WriteLine(treeParam.Value);
Console.WriteLine(privateParam.Value);

I think a move to EF4 is required sooner rather than later...

Sunday, March 28, 2010

Top 10 Treats in Entity Framework 4

Just finished watching Julia Lerman's video of the top 10 treats in EF4. These include pluralisation/singularisation improvements, foreign key support, new code generation features, lazy loading (enabled by default), T4 code generation, POCO support (convention over configuration), self tracking entities for WCF and new state management methods.

I've been using LINQ-to-SQL for my SQL Server based projects, primarily because the ASP.NET MVC demo's I had watched all used it and many blog posts warned me away from EF. Scott Gu created a quick EF model at the Guathon last week and I was surprised at how straightforward it seemed, so thought I would give it a go. I'm liking using EF and it's new features, however I'm a little concerned about investing in it when I know there are going to be many times when I have no choice but to use Oracle. I know there are some Oracle EF providers out there, but they need to be purchased, something which is difficult to justify after already investing heavily in the MS stack. Should NHibernate therefore be my ORM tool of choice - time to look into it a bit more and see how easy it is to use...