Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling stored procedure from Entity Framework 3.5

I"m using VS 2010 & EF 3.5. I've imported a stored procedure which returns a list of guids using the Function Import feature. How do I invoke it in my code? After instantiating the dbcontext, intellisense doesn't display the procedure I've imported. I know it's pretty easy in EF 4.0 but I'm stuck with EF 3.5 for this project. Any ideas on how get around this other than doing it the old-fashioned way?

like image 901
tempid Avatar asked Mar 11 '26 14:03

tempid


1 Answers

I don't think EF versions prior to 4 can use imported stored procedures that don't return entities. That is, your stored procedure must return a complete entity object in order for EF to use it. Since your procedure only returns a list of GUIDs, EF doesn't know how to use it.

You can put this in your partial data-context class to call the procedure:

    public IEnumerable<Guid> GetMyGUIDs()
    {
        if (this.Connection.State != System.Data.ConnectionState.Open)
            this.Connection.Open();
        var command = new System.Data.EntityClient.EntityCommand
        {
            CommandType = System.Data.CommandType.StoredProcedure,
            CommandText = @"YourContext.YourProcedureName",
            Connection = (System.Data.EntityClient.EntityConnection)this.Connection
        };
        var list = new List<Guid>();
        using (var reader = command.ExecuteReader())
        {
            // get GUID values from the reader here,
            // and put them in the list

            reader.Close();
        }
        return list;
    }
like image 106
ken Avatar answered Mar 14 '26 06:03

ken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!