Search code examples
c#.netsql-serverentity-frameworkentity-framework-6

Entity Framework 6 - How to determine stored procedure input parameters


I'm using EF6 and I've imported some stored procedures. Is there a way I can programmatically determine what the input parameters are for a particular procedure? I'd like to auto-generate some C# code to execute any stored procedure, but I first need to be able to determine what the input parameters are.

For example, I currently have imported a stored procedure named SellerModify. I have C# code to execute this stored procedure that looks, in part, as follows:

DbContext.Database.ExecuteSqlCommand("SellerModify @MarketName, @BankLocationCountryCode, @BankAccountOwnerName”, 
    new SqlParameter("@MarketName", seller.MarketName),
    new SqlParameter("@BankLocationCountryCode", seller.BankLocationCountryCode),                   
    new SqlParameter("@BankAccountOwnerName", seller.BankAccountOwnerName));

I'd like to autogenerate this code but I need to be able to determine the input parameters first.


Solution

  • What you're looking for is the equivelant of ADO.NET's SqlCommandBuilder.DeriveParameters() method. Unfortunately, to my knowledge, Entity Framework doesn't offer anything similar to this.

    That said, assuming a Microsoft SQL Server database, you could query the database beforehand to get the list of parameters. This can be done using something similar to the following SQL:

    SELECT      PARAMETER_NAME, 
                DATA_TYPE
    FROM        information_schema.parameters
    WHERE       specific_name = 'SellerModify'
    

    I know this isn't exactly what you're looking for, but it may provide an alternative approach for solving your problem.