【发布时间】:2023-03-08 18:45:01
【问题描述】:
我是 Entity Framework 的新手,我一直在 Entity Framework 中获得 EntityCommandCompilationException specified method not supported。我不知道为什么会引发这个异常。
我使用here. 发布的指南为我安装 MySQL 服务器 5.7 创建了一个自定义 UDF 聚合函数 my_Func() 它就像任何普通聚合函数一样工作,例如Sum() 会起作用。即我可以执行语句select my_Func(Column4) from db.table 并将所需结果作为double 返回。我已经对其进行了测试,它可以在 MySQL 服务器中运行。我希望能够在 linq to entity 查询中使用此方法,为了做到这一点,我做了以下事情。
using (var context = new dbEntities())
{
var results = from items in context.table
group items by new
{ items.Column1, items.Column2 } into groupingItem
select new OutputType()
{
GroupedResult = groupingItem.OrderBy(x => x.Column3).Select(x => x.Column4).my_Func()
};
}
我创建了一个包含该方法的静态类。
public static class ModelDefinedFunctions
{
[DbFunction("dbModel.Store", "my_Func")]
public static double my_Func(this IEnumerable<double> items)
{
throw new NotSupportedException("Direct calls are not supported.");
}
}
在 .edmx 文件中我手动添加了以下标签
<Function Name="my_Func" ReturnType="double" Aggregate="true"
BuiltIn="false" NiladicFunction="false"
IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="db">
<Parameter Name="value" Type="Collection(double)" Mode="In" />
</Function>
【问题讨论】:
-
它解决了你的问题stackoverflow.com/a/27029331/6733826 吗?
-
不,它没有。我没有命令文本,并且在我的代码中将 iscomposable 设置为 true。
-
也许我是个傻瓜,但是数据库方案真的是“db”,而不是“dbo”吗?
-
我愿意接受涉及实体框架的其他建议或解决方法;
-
我更喜欢 Code first 的方式,fluent api,... 好久没用 edmx 了,所以不能给你建议抱歉跨度>
标签: c# mysql entity-framework linq-to-entities entity-framework-6