DbFunction “cannot be translated into a LINQ to Entities store expression”

北城以北 提交于 2019-12-12 12:05:42

问题


I'm trying to access database funciton 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 a class called EntityFunction to call Functions in 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 to be 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)' on the type 'MySqlEntityFramework.Models.EntityFunctions' cannot be translated into a LINQ to Entities store expression

Could anyone help me to solve this problem please ?

Thank you,

来源:https://stackoverflow.com/questions/45451383/dbfunction-cannot-be-translated-into-a-linq-to-entities-store-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!