【问题标题】:DbFunction "cannot be translated into a LINQ to Entities store expression"DbFunction“无法转换为 LINQ to Entities 存储表达式”
【发布时间】:2018-01-09 02:54:21
【问题描述】:

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

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

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

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

这是我的 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
    }

一切似乎都很好,但是当我使用这段代码时:

        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 存储表达式

谁能帮我解决这个问题?

谢谢,

【问题讨论】:

  • 搜索错误“无法转换为 LINQ to Entities 存储表达式”并阅读。另外,请只选择 一个 版本。 EF4 还是 EF6?它们是不同的。
  • 消息很明显:您不能在 LINQ to Entities SELECT 查询中使用Echo 方法。即使使用了DbFunctionAttributeEcho 方法的返回类型也是错误的 - 要在 LINQ to Entities 中使用自定义扩展方法,该方法必须返回 IQueryable&lt;T&gt;
  • @user2864740,你能给我一个链接吗?我在 Stackoverflow 上搜索过所有帖子,其中大部分是关于存储过程的。
  • 也许这会对你有所帮助:stackoverflow.com/a/58136154/10406502

标签: c# linq sql-server-2012 entity-framework-4 entity-framework-6


【解决方案1】:

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

在 OnModelCreating() 方法中,添加这一行:

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

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

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

我的想法来自 this link

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 2015-04-02
    • 1970-01-01
    相关资源
    最近更新 更多