How can I permanently fix the “EdmFunctionAttribut

2019-06-14 09:03发布

I am getting the warning "EdmFunctionAttribute is obsolete" after I have upgraded a database first project from EF4 to EF 6.1.3:

'System.Data.Entity.Core.Objects.DataClasses.EdmFunctionAttribute' is obsolete: 'This attribute has been replaced by System.Data.Entity.DbFunctionAttribute.' C:\{myProjectPath}\DataContextEntityObjects.cs

The attribute is used in various places like

    [EdmFunction("DataContext", "Split")]
    public IQueryable<Split_Result> Split(global::System.String rowData, global::System.String splitOn)
    {
    // ... auto-generated code ...
    }

If you fix that in the *.cs-files mentioned by the error message, then each time when the model gets updated via update the model from database, the error comes up again.

How can this issue permanently be fixed (so a model update doesn't bring it up again)?

1条回答
在下西门庆
2楼-- · 2019-06-14 09:31

The file DataContextEntityObjects.cs is auto-generated from DataContextEntityObjects.tt and uses the attribute in various places like

    [EdmFunction("DataContext", "Split")]
    public IQueryable<Split_Result> Split(global::System.String rowData, global::System.String splitOn)
    {
    // ... auto-generated code ...
    }

Since it is auto-regenerated each time when the model gets updated via update the model from database, the solution was to modify the T4 template as follows:

I've identified the relevant part in the T4 file starts here (lines 214-283):

    ////////
    ////////  Write EntityContainer and ObjectContext Function Import methods.
    ////////
    region.Begin(CodeGenerationTools.GetResourceString("Template_RegionFunctionImports"));
    foreach (EdmFunction edmFunction in container.FunctionImports)
    {

        IEnumerable<FunctionImportParameter> parameters = FunctionImportParameter.Create(edmFunction.Parameters, code, ef);

As suggested in the template How to: Customize Object Layer Code Generation and Generating Artifacts by Using Text Templates, the template can be modified to generate the right code.

Do the following:

  1. Open up the DataContextEntityObjects.tt file
  2. Search and replace the following:

    EdmFunction replacement

  3. Save the file DataContextEntityObjects.tt

From now on, each time you update the model, this will generate the corresponding *.cs file, create the attribute inside correctly and hence the obsolete warning goes away.

查看更多
登录 后发表回答