DbFunction "cannot be translated into LINQ to Entities store expression"

I am trying to access database functions using linq to sql. Herer is my SQL scalar function:

CREATE  FUNCTION    Echo(@text NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)       AS
BEGIN
   RETURN @text;
END;

      

I created an EntityFunction class to call functions on Sql server:

    public static class EntityFunctions
    {
        [DbFunction("SqlServer", "Echo")]
        public static string Echo(string parameter)
        {
            throw new NotImplementedException();
        }
    }

      

And here is my DbContext:

    public class MainDbContext : DbContext
    {
        #region Properties

        /// <summary>
        /// List of accounts in database.
        /// </summary>
        public DbSet<Account> Accounts { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// Initiate context with default settings.
        /// </summary>
        public MainDbContext() : base(nameof(MainDbContext))
        {

        }

        #endregion

        #region Methods

        /// <summary>
        /// Called when model is being created.
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
            base.OnModelCreating(modelBuilder);
        }

        #endregion
    }

      

Everything seems fine, but when I used this code:

        private static void Main(string[] args)
        {
            var context = new MainDbContext();
            var accounts = context.Accounts.Select(x => EntityFunctions.Echo(x.Email)).ToList();

        }

      

Application threw me an exception: The specified method "System.String Echo (System.String)" in type "MySqlEntityFramework.Models.EntityFunctions" could not be translated into a LINQ to Entities storage expression

Can anyone help me solve this problem?

Thank,

+3


source to share





All Articles