A stored procedure in Entity Framework is not accessible.

c# entity-framework stored-procedures

Question

I added a reference to a stored procedure in my edmx file, then right clicked on it and selected "Create Function Import", it was added to the Function Imports folder under EntityContainer in the model browser.

As I understand it I should be able to use it like so:

sampleEntities db = new sampleEntities();
db.SampleStoredProcedure();

but it does not show up on the db object. Is there a step I'm missing? The Function Import is set to public, has no return value, and one parameter that I can see when I expand it.

1
3
2/11/2010 10:36:31 PM

Accepted Answer

Does your stored procedure return a simple (that is to say scalar) value? If so, the designer will not generate the code for you:

If the Return Type is set to a simple type, Visual Basic or C# is not automatically generated for the Function Import.

However, this has been fixed in the newest version of the Entity Framework:

You can select the None and Scalar return types as you could before. However, when the “Function Import” is created, some new code is injected into the Model code behind file that materializes the stored procedure into an operation on the ObjectContext itself.

7
2/11/2010 10:53:08 PM

Popular Answer

You could execute stored procedure with EntityCommand class, add a new stored procedure to entity data model, then add it as a function and name it, then execute the stored procedure:

public bool ExecuteCMD(string Command) 
{
    using (var entities = new DbEntities())
    {
        var conn = new System.Data.EntityClient.EntityConnection(entities.Connection.ConnectionString);
        try
        {
            conn.Open();
            using (EntityCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = "DbEntities." + Command;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
            }
        }
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow