0

I have a stored procedure that I am calling through LINQ and EF, it is returning a count, and the Stored procedure is tried and tested.

It requires 10 parameters.

These are the parameters the stored procedure takes:

@StartDate DATETIME,
@EndDate DATETIME,
@Wards CodeTable READONLY,
@Clinicians CodeTable READONLY,
@Sites CodeTable READONLY,
@Specialities CodeTable READONLY,
@excludeCurrentInpatients BIT,
@DemoMode BIT,
@SortOrder Int,
@IsCurrent bit

And this is the way I call it in LINQ:

public int Inpatient_Count(DateTime? startDate, DateTime? endDate, List<string> wards, List<string> clinicians, List<string> sites, List<string> specialities, bool excludeCurrentInpatients, bool demoMode, int sortOrder, bool IsCurrent)
{
    using (DBName context = new DBName())
    {
        int total = context.SPROC_Name(startDate, endDate, wards, clinicians, sites, specialities, excludeCurrentInpatients, demoMode, sortOrder, IsCurrent);
        return total;
    }
}

I keep getting the error:

No Overload method for 'SPROCName' takes 10 arguments

but I am passing ten parameters in. Any help would be greatly appreciated.

GSerg
  • 76,472
  • 17
  • 159
  • 346
S.Gray
  • 69
  • 6
  • 1
    That the procedure has 10 parameters in the DB doesn't mean your code model is also properly reflecting that. Try refreshing your model. If that doesn't work, double check that your code and you are looking at the same database. – Jeroen Mostert Dec 04 '19 at 15:53
  • 2
    This is not `LINQ`, just normal use of EF. Also, I'm not sure that you can pass in lists directly. See [here](https://stackoverflow.com/questions/5595353/how-to-pass-table-value-parameters-to-stored-procedure-from-net-code). – Peter Smith Dec 04 '19 at 15:56
  • 1
    Thanks both, i refreshed the model and it has gave me another warning now saying entity framework does not support (table types). – S.Gray Dec 04 '19 at 16:08
  • Are you in Core? Or Framework? – Casey Crookston Dec 04 '19 at 16:13
  • im in Framework – S.Gray Dec 04 '19 at 16:15

1 Answers1

0

You could do something like this:

    public bool Import(IEnumerable<SqlDataRecord> entities, string storedProcedureName)
    {
        var paramDate = new SqlParameter("@StartDate", SqlDbType.DateTime)
        {
            Value = DateTime.Now
        }; 

        var paramTable = new SqlParameter("@Wards", SqlDbType.Structured)
        {
            TypeName = "CodeTable",
            Value = entities.ToList()
        };

        var returnValue = _dbContext.Database.ExecuteSqlCommand($"{storedProcedureName} @StartDate, @Wards", paramDate, paramTable);
        return true;

    }

Modify this to fit your needs, and supply needed parameters.

rjs123431
  • 688
  • 4
  • 14