What is the preferable way to call a stored procedure in an async await way ?

mehmood tekfirst 771 Reputation points
2022-04-14T08:19:02.337+00:00

Hi,

I am migrating from EF 6 to EF Core 6.

Now the first thing , I need to know is to migrate my Stored Procedure call .

How can I create an utility/extension method to call a Store Procedure in an Async Await way with in and out type parameters ?

and Where would be the connection string ?

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

Accepted answer
  1. AgaveJoe 26,166 Reputation points
    2022-04-15T14:12:20.61+00:00

    and Currently I am getting this error message in catch block Data is Null. This method or property cannot be called on Null values.

    The error usually means there null values in the SQL table but the model design does not have an associated nullable type. Please see the reference documentation.

    Entity Properties

    I want to use Either FromSqlInterpolated or FromSqlRaw . Can we fix it ?

    I showed you how to handle a nullable SQL input parameter above. Your second code sample does use a null input parameter so I'm not sure what problem you are having. Perhaps reread the docs to learn how to create a parameter in C#.

    https://learn.microsoft.com/en-us/ef/core/querying/raw-sql#passing-parameters

    1 person found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. mehmood tekfirst 771 Reputation points
    2022-04-18T11:03:36.493+00:00

    Thank you.
    Your suggestion worked for me.

    I have substituted the passing null value and

    I allowed the null properties in model then it started to work.

    [Keyless]
        public class FranchiseWebInquiry
        {
            public int? FranchisesId { get; set; }
            public int? SubOfficeId { get; set; }
            public string? FranchiseName { get; set; }
            public string? SubOfficeName { get; set; }
            public string? Code { get; set; }
            public string? PickupId { get; set; }
            public string? Address1 { get; set; }
            public string? Address2 { get; set; }
            public string? Address3 { get; set; }
            public string? PostCode { get; set; }
            public string? PrimaryContact { get; set; }
            public string? Email { get; set; }
            public string? Town { get; set; }      
            public double? Latitude { get; set; }
            public double? Longitude { get; set; }
    
        }
    
    0 comments No comments