EntityFrameWork Core - Stored Procedure

Mohammad Radawan 106 Reputation points
2022-03-16T21:32:51.417+00:00

Every time I execute this code from Swagger I get the same error! Must declare the scalar variable "@typecard"

            var TypeCardp = new SqlParameter("@typecard", System.Data.SqlDbType.Int).Value = agent.TypeCard;  
            var AgentNamep = new SqlParameter("@Agentname", System.Data.SqlDbType.NVarChar).Value =   
            agent.AgentName;  
            var Birthday = new SqlParameter("@Birthday", System.Data.SqlDbType.DateTime2).Value = agent.Birthdate;  
            var Gender1 = new SqlParameter("@Gender1", System.Data.SqlDbType.Bit).Value = agent.Gender1;  
            var Gender2 = new SqlParameter("@Gender2", System.Data.SqlDbType.Bit).Value = agent.Gender2;  
            var SubscriptionTitle = new SqlParameter("@SubscriptionTitle",                                   
            System.Data.SqlDbType.UniqueIdentifier).Value = agent.SubscriberTitle;  
            var Phone = new SqlParameter("@Phone", System.Data.SqlDbType.NVarChar).Value = agent.Phone;  
            var Password = new SqlParameter("@Password", System.Data.SqlDbType.NVarChar).Value =   
            agent.LoginPassword;  
            var CityGuide = new SqlParameter("@CityGuide", System.Data.SqlDbType.UniqueIdentifier).Value =   
            agent.CityGuide;  
            var DistrictGuide = new SqlParameter("@DistrictGuide", System.Data.SqlDbType.UniqueIdentifier).Value =   
            agent.DistrictGuide;  
            var InstituteGuide = new SqlParameter("@InstituteGuide", System.Data.SqlDbType.UniqueIdentifier).Value =   
            agent.InstituteGuide;  
            var NearestPoint = new SqlParameter("@NearestPoint", System.Data.SqlDbType.NVarChar).Value =   
            agent.NearestPoint;  
            var FatherName = new SqlParameter("@FatherName", System.Data.SqlDbType.NVarChar).Value =   
            agent.FatherName;  
            var FLatitude = new SqlParameter("@FLatitude", System.Data.SqlDbType.Float).Value = agent.FixedLatitude;  
            var FLongitude = new SqlParameter("@FLongitude", System.Data.SqlDbType.Float).Value =   
            agent.FixedLongitude;  
            var Subscriptioner = new SqlParameter("@Subscriptioner", System.Data.SqlDbType.UniqueIdentifier).Value   
             = agent.Subscriptioner;  
            var NotificationChannel = new SqlParameter("@NotificationChannel",   
            System.Data.SqlDbType.NVarChar).Value = agent.NotificationChannel;  
            var CarName = new SqlParameter("@CarName", System.Data.SqlDbType.NVarChar).Value = agent.CarName;  
            var NumberOfSeats = new SqlParameter("@NumberOfSeats", System.Data.SqlDbType.Int).Value =   
            agent.NumberOfSeats;  
            var Color = new SqlParameter("@Color", System.Data.SqlDbType.UniqueIdentifier).Value = agent.Color;  
            var SubBrand = new SqlParameter("@SubBrand", System.Data.SqlDbType.UniqueIdentifier).Value =   
            agent.SubBrand;  
            var FuelType0 = new SqlParameter("@FuelType0", System.Data.SqlDbType.Bit).Value = agent.FuelType0;  
            var FuelType1 = new SqlParameter("@FuelType1", System.Data.SqlDbType.Bit).Value = agent.FuelType1;  
            var DocumentNumber = new SqlParameter("@DocumentNumber", System.Data.SqlDbType.NVarChar).Value   
            = agent.DocumentNumber;  
            var BrandGuide = new SqlParameter("@BrandGuide", System.Data.SqlDbType.UniqueIdentifier).Value =   
            agent.BrandGuide;  
  
            context.Database.ExecuteSqlRaw("exec Prc_InsertAgent @typecard, @Agentname, @Birthday, @Gender1,   
                @Gender2, @SubscriptionTitle, @Phone, @Password, @CityGuide, @DistrictGuide, @InstituteGuide,   
                @NearestPoint, @FatherName, @FLatitude, @FLongitude, @Subscriptioner, @NotificationChannel,   
                @CarName, @NumberOfSeats, @Color, @SubBrand, @FuelType0, @FuelType1, @DocumentNumber,   
                @BrandGuide", TypeCardp, AgentNamep, Birthday, Gender1, Gender2, SubscriptionTitle, Phone,   
                Password, CityGuide, DistrictGuide, InstituteGuide, NearestPoint, FatherName, FLatitude, FLongitude,   
               Subscriptioner, NotificationChannel, CarName, NumberOfSeats, Color, SubBrand, FuelType0, FuelType1,   
               DocumentNumber, BrandGuide);  

I'm using the same approach to execute a SP with less fields than this one

            public void AddCity(City CityName)  
            {  
                //throw new NotImplementedException();  
                var param = new SqlParameter("@cardname", CityName.Name);  
                var param1 = new SqlParameter("@latitude", CityName.Latitude);  
                var param2 = new SqlParameter("@longitude", CityName.Longitude);  
                context.Database.ExecuteSqlRaw("exec AddCity @cardname, @latitude,   
               @longitude", param, param1, param2);  
            }  

and it's working perfectly

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
741 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 28,291 Reputation points
    2022-03-17T11:09:19.877+00:00

    Make sure agent.TypeCard is not null. Set a breakpoint and use the watch window or immediate window to check the agent property value(s).


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.