Ejemplo n.º 1
0
        private static void ExecuteSpWithOutParameterTest()
        {
            var proc = @"
            create procedure uspDeleteBikeWithColorOut(@BikeId int, @Color varchar(30) output)
            as begin
	            select @Color = [color] from [rentals].[Vehicles] where [VehicleId] = @BikeId;
	            delete from [rentals].[Vehicles] where [VehicleId] = @BikeId;
            end
            ";

            using (var context = new BikeRentalContext())
            {
                context.Bikes.Add(new Bike
                {
                    BikeType = BikeType.City,
                    Color    = "Blue!!!",
                    Number   = "B124"
                });

                context.SaveChanges();
                context.Bikes.ToList().Dump();

                var bikeToDelete = context.Bikes.FirstOrDefault(x => x.Number == "B124");

                if (bikeToDelete != null)
                {
                    var idParameter     = new SqlParameter("BikeId", bikeToDelete.VehicleId);
                    var outputParameter = new SqlParameter
                    {
                        ParameterName = "Color",
                        DbType        = DbType.String,
                        Size          = 30,
                        Direction     = ParameterDirection.Output
                    };

                    context.Database.ExecuteSqlCommand("uspDeleteBikeWithColorOut @BikeId, @Color OUTPUT", idParameter, outputParameter);

                    context.SaveChanges();
                    outputParameter.Dump();
                    context.Bikes.ToList().Dump();
                }
            }
        }