Search code examples
entity-framework-6automappercode-firstprojectionudf

How could one use Automapper with EntityFramework 6.1.3 to project custom (UDF) DbFunciton calls?


I am using this Nuget Library:

EntityFramework.CodeFirstStoreFunctions

Have this code:

public abstract class DbCore : DbContext {
     [DbFunction("CodeFirstDatabaseSchema", "DecodeBase64")]
     public static string DbDecodeBase64(string encoded) {
          throw new NotSupportedException();
     }

     protected override void OnModelCreating(DbModelBuilder modelBuilder) {
          modelBuilder.Conventions.Add(new FunctionsConvention("dbo"));
          base.OnModelCreating(modelBuilder);
     }
}

When I run this code using projections:

IMappingExpression mapperExpression = ...(omitted)
mapperExpression.ForMember(dest => dest.ContactNote, opt => opt.MapFrom(entity => DbCore.DbDecodeBase64(entity.ContactNote)));

Then I have a SQL function (UDF) called: DecodeBase64


Solution

  • Everything I was doing was correct, but my LINQ was producing an object that was no longer IQueryable, which was causing these functions not to be called/generated as SQL.

    So, in short. The question is the answer and if you are having any issues with this, make sure your still using IQueryable objects while calling these functions.