Friday, March 5, 2010

Enitity Framework CAN Support Stored Procedures That Do Not Have A Return Value

I didn't realize this, but apparently there articles that state that EF does not support procedures that return no result. This surprised me because I've been doing that in my application.

To Do So:
When you import your objects, make sure the stored proecure is imported. Next go to your Model Browser, and go the the EntityContainer in your Model. Right click Function Imports and add the name of your stored procedure.


Once you have done that, you can call the procedure through EF in the following manner:

//Create the Context then set up a DbCommand.
YourEntityModel.sprocContext = new YourEntityModel.();
DbCommand command = sprocContext.Connection.CreateCommand();
command.CommandText = "YourEntityModel.yourStoredProcedure";
command.CommandType = CommandType.StoredProcedure;
//If parameters are needed
DbParameter parameter = command.CreateParameter();
parameter.DbType = DbType.Int32;
parameter.ParameterName = parameterName;
parameter.Value = value;
command.Parameters.Add(parameter);
//Excecute the command
DbCommand nonQuery = (DbCommand)nonQueryObject;
nonQuery.Connection.Open();
nonQuery.ExecuteNonQuery();

It is as simple as that.

No comments: