开发者

How to call Stored Procedure from Entity Framework which have no return data?

开发者 https://www.devze.com 2023-04-12 09:16 出处:网络
I\'m trying to use stored procedure in my Entity Framaework 4. I have created entity model and can see the stored proc from the Model Browser 开发者_StackOverflow中文版in the Stored Procedure folder

I'm trying to use stored procedure in my Entity Framaework 4.

I have created entity model and can see the stored proc from the Model Browser 开发者_StackOverflow中文版in the Stored Procedure folder.

I added the stored proc through functional import. My stored proc has no return type. Now when I'm trying to call the stored proc in the object context (Business Layer ) I can't see the stored proc in the intelisence. Is ther e any mistake I'm doing?

Note: I can see other procedures if they have return values . Here in this specilal case that do not have any return values


The POCO generating template for some reason ignores function imports that have no return type. I really don't know why they did that, but you can fix it by changing the template. I remember that I did this too before I dropped the whole EF-POCO thing. Do you use POCOs with EF? To fix this you have to open the .tt template file. Basically I just copied the loop for the normal function imports and changed it a bit. What I added was

foreach (EdmFunction edmFunction in container.FunctionImports)
{
    var parameters = FunctionImportParameter.Create(edmFunction.Parameters, code, ef);
    string paramList = String.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray());
    if (edmFunction.ReturnParameter != null)
    {
        continue;
    }

#>
<#=Accessibility.ForMethod(edmFunction)#> void <#=code.Escape(edmFunction)#>(<#=paramList#>)
{
<#
    foreach (var parameter in parameters)
    {
        if (!parameter.NeedsLocalVariable)
        {
            continue;
        }
#>

    ObjectParameter <#=parameter.LocalVariableName#>;

    if (<#=parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null"#>)
    {
        <#=parameter.LocalVariableName#> = new ObjectParameter("<#=parameter.EsqlParameterName#>", <#=parameter.FunctionParameterName#>);
    }
    else
    {
        <#=parameter.LocalVariableName#> = new ObjectParameter("<#=parameter.EsqlParameterName#>", typeof(<#=parameter.RawClrTypeName#>));
    }
<#
    }
#>
    base.ExecuteFunction("<#=edmFunction.Name#>"<#=code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()))#>);
}
<#
}

You can find a related q/a here: FunctionImport in entity framework 4 issue


You need to map the procedre to an update read or delete operation otherwise no sense in mapping it. The alternative is the execute it directly on the context.

0

精彩评论

暂无评论...
验证码 换一张
取 消