Entity Framework 6 stored procedures default values

1.2k views Asked by At

I have an existing database and I am using Entity Framework 6 to access it. All seems to be fine so far except that I am using an existing stored procedure with the following definition

CREATE PROCEDURE [dbo].[spCreateAlternativeQuote] 
     @PolicyID int, 
     @NewPolicyID int output, 
     @newSchemeId int = NULL, 
     @UserID varchar (50) = NULL, 
     @TemplateID int = NULL 
     ............

This has generated ok under the Entities of the .edmx model file, with one exception: the @UserID parameter has not been set to nullable even though it has a default value of NULL.

This is the what's being generated by EF

public virtual int spCreateAlternativeQuote(Nullable<int> policyID, ObjectParameter newPolicyID, Nullable<int> newSchemeId, string userID, Nullable<int> templateID)
    {
        var policyIDParameter = policyID.HasValue ?
            new ObjectParameter("PolicyID", policyID) :
            new ObjectParameter("PolicyID", typeof(int));

        var newSchemeIdParameter = newSchemeId.HasValue ?
            new ObjectParameter("newSchemeId", newSchemeId) :
            new ObjectParameter("newSchemeId", typeof(int));

        var userIDParameter = userID != null ?
            new ObjectParameter("UserID", userID) :
            new ObjectParameter("UserID", typeof(string));

        var templateIDParameter = templateID.HasValue ?
            new ObjectParameter("TemplateID", templateID) :
            new ObjectParameter("TemplateID", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("spCreateAlternativeQuote", policyIDParameter, newPolicyID, newSchemeIdParameter, userIDParameter, templateIDParameter);
    }

Now I know that string in c# can be null but when trying to call the auto generated code and passing null I get an error saying I am passing an incorrect type in the parameter list. See below

spCreateAlternativeQuote(originalPolicyId,newPolId, newSchemeId,null,null);

Has anyone ever had this issue before or am I just doing something stupid? I don't have much experience with EF so it's entirely possible that I've done something wrong.

I should add that I don't have any control over the database so changing the stored procedure isn't an option.

Any help would be much appreciated.

Thanks

Mike

0

There are 0 answers