How can I permanently fix the "EdmFunctionAttribute is obsolete" warning, which occurs after upgrading to EF6?

c# entity-framework-4 entity-framework-6 migration visual-studio

Question

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
0
4/4/2019 1:50:00 PM

Accepted Answer

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.

2
4/5/2019 7:22:36 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow