简体   繁体   English

在实体框架中映射/使用内置的MySQL函数

[英]Map/use built-in MySQL function in Entity Framework

Quite a simple question, I hope: I want to use built-in MySQL functions just as YEARWEEK or INSERT in Entity Framework 6 (similar to the System.Data.Entity.DbFunctions namespace). 我希望这是一个简单的问题:我想像Entity Framework 6中的YEARWEEK或INSERT一样使用内置的MySQL函数(类似于System.Data.Entity.DbFunctions命名空间)。 Is there a way to add a mapping to those functions? 有没有办法向这些功能添加映射?

I already tried to add them via the edmx file, but that didn't work quite right. 我已经尝试过通过edmx文件添加它们,但是效果不佳。

<!-- edmx:ConceptualModels -->
<Function Name="YearWeek" ReturnType="String">
    <Parameter Name="date" Type="DateTime" />
    <DefiningExpression>
        YEARWEEK(date, 3)
    </DefiningExpression>
</Function>

<!-- edmx:StorageModels -->
<Function Name="YEARWEEK" IsComposable="true" ReturnType="varchar" BuiltIn="true" Aggregate="false" NiladicFunction="false" ParameterTypeSemantics="AllowImplicitConversion">
    <Parameter Name="date" Type="datetime" Mode="In" />
    <Parameter Name="mode" Type="int" Mode="In" />
</Function>

And in my c# code: 在我的C#代码中:

[System.Data.Entity.DbFunction("otrsModel", "YearWeek")]
public static string YearWeek(DateTime date) {
    throw new NotSupportedException("Direct calls are not supported.");
}

This, right now, throws me a System.Data.Entity.Core.EntityCommandCompilationException . 现在,这将引发System.Data.Entity.Core.EntityCommandCompilationException The inner exception is: "'YEARWEEK' cannot be resolved into a valid type or function." 内部的例外是: “'YEARWEEK'无法解析为有效的类型或函数。”

However, calling the following code on that same database works just fine: 但是,在同一数据库上调用以下代码也可以:

var week = db.Database.SqlQuery<dynamic>("SELECT INSERT(YEARWEEK(create_time, 3), 5, 0, '/'), ticket.* AS a FROM ticket").ToList();

Any idea what's wrong here? 知道这里有什么问题吗?

I finally solved the problem, and the solution is quite simple: Adding a definition to edmx:ConceptualModels is unnecessary. 我终于解决了问题,解决方案非常简单:不需要向edmx:ConceptualModels添加定义。 You just have to add the edmx:StorageModels definition and call it correctly. 您只需要添加edmx:StorageModels定义并正确调用即可。 Here's my modified code with exemplary implementations of MySQL's built-in functions INSERT and YEARWEEK : 这是我经过修改的代码,其中包含MySQL内置函数INSERTYEARWEEK的示例性实现:

<!-- edmx:StorageModels -->
<Function Name="YEARWEEK" IsComposable="true" ReturnType="varchar" BuiltIn="true" Aggregate="false" NiladicFunction="false" ParameterTypeSemantics="AllowImplicitConversion">
  <Parameter Name="date" Type="datetime" Mode="In" />
  <Parameter Name="mode" Type="int" Mode="In" />
</Function>
<Function Name="INSERT" IsComposable="true" ReturnType="varchar" BuiltIn="true" Aggregate="false" NiladicFunction="false" ParameterTypeSemantics="AllowImplicitConversion">
  <Parameter Name="str" Type="varchar" Mode="In" />
  <Parameter Name="position" Type="int" Mode="In" />
  <Parameter Name="number" Type="int" Mode="In" />
  <Parameter Name="substr" Type="varchar" Mode="In" />
</Function>

And the corresponding c# code: 以及相应的C#代码:

namespace MySQL_3 {
    class Program {
        static void Main(string[] args) {
            var db = new myEntities();

            var test = db.ticket.Select(t => t.change_time.YearWeek(3).Insert(5, 0, "/"));

            var test2 = test.ToList();

            Console.Read();
        }
    }

    public static class BuiltInFunctions {

        [DbFunction("myModel.Store", "YEARWEEK")]
        public static string YearWeek(this DateTime date, Int32 mode) => throw new NotSupportedException("Direct calls are not supported.");

        [DbFunction("myModel.Store", "INSERT")]
        public static string Insert(this string str, int position, int number, string substr) => throw new NotSupportedException("Direct calls are not supported.");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM