Tyddlywink
Tyddlywink

Reputation: 891

Entity Framework Stored Procedure with Multiple Record Set not accepting my parameter

I'm using Microsofts suggested solution for using Entity Framework to read multiple record sets from a stored procedure but added a small snippet to use parameters and it's not working. I've had a co-worker look at the code and tell me it looks like it should work so I thought I'd ask here.

Using the 4.5 framework is not an option. I'm stuck with 4.0 and etity framework 4.4.

            App MyApp = (App)Application.Current;
            EnterpriseEntities EE = new EnterpriseEntities();
            EE.Database.Connection.ConnectionString = MyApp.EnterpriseEntityConnectionString;

            var cmd = EE.Database.Connection.CreateCommand();
            cmd.CommandText = "[dbo].[spSelectWaterUsesByRightID]";
            var param = cmd.CreateParameter();


            param.Direction = ParameterDirection.Input;
            param.DbType = DbType.Int32;
            param.ParameterName = "@RightID";
            param.Value = this.RightID;

            cmd.Parameters.Add(param);


            EE.Database.Connection.Open();
            var reader = cmd.ExecuteReader();

            List<WaterUses> ListOfWaterUses = (((System.Data.Entity.Infrastructure.IObjectContextAdapter)EE)
                    .ObjectContext
                    .Translate<WaterUses>(reader, "WaterUses",System.Data.Objects.MergeOption.AppendOnly)).ToList(); 

When I get to the ExecuteReader line I get an error message that the stored procedure requires Parameter @RightID but that's what I'm passing. I checked the parameter count right before it executes and it's at 1.

Upvotes: 0

Views: 914

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109118

You have to add

cmd.CommandType = CommandType.StoredProcedure;

somewhere before cmd.ExecuteReader().

Upvotes: 1

Related Questions