DbFunction“无法转换为LINQ to Entities存储表达式”。 [英] DbFunction "cannot be translated into a LINQ to Entities store expression"

查看:295
本文介绍了DbFunction“无法转换为LINQ to Entities存储表达式”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用linq to sql访问数据库功能。这是我的SQL标量函数:

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;

我创建了一个名为EntityFunction的类来调用Sql Server中的函数:

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();
        }
    }

这是我的DbContext:

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();

        }

应用程序抛出异常:指定的方法类型'MySqlEntityFramework.Models.EntityFunctions'上的'System.String Echo(System.String)'无法转换为LINQ to Entities存储表达式

有人可以帮我解决这个问题吗?

Could anyone help me to solve this problem please ?

谢谢

推荐答案

这是使它对我有用的修改。

Here's are the modifications that got it working for me.

在OnModelCreating()方法中,添加以下行:

In the OnModelCreating() method, add this line:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // ...
        modelBuilder.Conventions.Add(new FunctionsConvention("dbo", this.GetType()));
    }

定义DbFunction存根时,请使用以下属性:

When defining the DbFunction stub use these attributes:

    [DbFunction("CodeFirstDatabaseSchema", "Echo")]
    public static string Echo(string text)
    {
        throw new NotSupportedException("Direct calls are not supported.");
    }

我从
中获得了想法此链接

这是我执行的测试看看这是否是您遇到的问题:如果我注释掉modelBuilder.Conventions.Add上面的行,则会收到与您收到的错误类似的错误。

Here's the test I performed to see if it was the issue you were having: If I comment out the modelBuilder.Conventions.Add line above, I receive a similar error to what you were receiving.


System.NotSupportedException:无法将类型为 try1.ExContext的指定方法 System.String Echo(System.String)转换为LINQ to Entities存储表达式。

System.NotSupportedException: 'The specified method 'System.String Echo(System.String)' on the type 'try1.ExContext' cannot be translated into a LINQ to Entities store expression.'

这篇关于DbFunction“无法转换为LINQ to Entities存储表达式”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆