【问题标题】:Stored procedure call from Enitiy Framwork 5 to MySQL从 Entity Framework 5 到 MySQL 的存储过程调用
【发布时间】:2017-12-28 12:04:07
【问题描述】:

我正在尝试从 Entity fromawork 5.0 到 MySQL 进行存储过程调用。我能够连接到 MySQL,但是当我调用存储过程时,我得到了空实体(没有数据)。我该如何解决这个问题 -

下面是我用来调用存储过程的代码 -

public virtual ObjectResult<usp_GetFileType_Result> usp_GetFileType()
        {
            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<usp_GetFileType_Result>("usp_GetFileType");
        }

我观察到更多的事情是,每当我从存储的 porcedure 更新 .edmx 文件时,我都会得到这样的空模型 -

namespace MySQLwithEntity
    {
        using System;

        public partial class usp_GetFileType_Result
        {

        }
    }

所以我像下面这样手动填写 -

namespace MySQLwithEntity
    {
        using System;

        public partial class usp_GetFileType_Result
        {
          public int FileTypeId { get; set; }
          public string FileTypeName { get; set; }
        }
    }

但我仍然得到空值 -

【问题讨论】:

    标签: c# mysql entity-framework


    【解决方案1】:

    搜索了几个小时后,我找到了解决方案。它可能对某人有帮助 -

    上述从 MySQL 调用 SP 的方式不起作用的原因是自动生成的实体模型是空白的,.edmx 文件没有为从 MySQL SP 返回的列生成映射。

    namespace MySQLwithEntity
        {
            using System;
    
            public partial class usp_GetFileType_Result
            {
    
            }
        }
    

    手动放置实体炎 -

    namespace MySQLwithEntity
        {
            using System;
    
            public partial class usp_GetFileType_Result
            {
              public int FileTypeId { get; set; }
              public string FileTypeName { get; set; }
            }
        }
    

    在 .edmx 文件中手动创建映射,如下所示,虽然不推荐,但我别无选择。当您将 .edmx 文件作为 xml 文件打开时,必须针对“CSDL 内容”部分和“C-S 映射内容”部分进行编辑 -

     <FunctionImportMapping FunctionImportName="usp_GetFileType" FunctionName="Model1.Store.usp_GetFileType">
                  <ResultMapping>
                  <ComplexTypeMapping TypeName="Model1.usp_GetFileType_Result">
                    <ScalarProperty Name="FileTypeId" ColumnName="FileTypeId" />
                    <ScalarProperty Name="FileTypeName" ColumnName="FileTypeName" />
                  </ComplexTypeMapping>
                </ResultMapping>
              </FunctionImportMapping>
    

    其次是调用SP的代码。我将代码更改为调用 SP,如下所示 -

    public virtual IList<usp_GetFileType_Result> GetCustOrderHist()
            {
                IList<usp_GetFileType_Result> data = ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<usp_GetFileType_Result>("CALL usp_GetFileType();").ToList();
    
                return data;
            }
    

    MySQL 中的Call 相当于 MS SQL 中的EXEC

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      相关资源
      最近更新 更多