c# - 'System.Data.Objects.ObjectContext'不包含带有0个参数的

我继承了一些代码(经常发生!),看起来有点像这样:(省略了命名空间)

public partial class SpatialDatabase : global::System.Data.Objects.ObjectContext
{
    public string MY_PROCEDURE(Decimal arg1, Decimal arg2)
    {
        using (EntityConnection conn = new EntityConnection(this.Connection.ConnectionString))
        {
            conn.Open();

            object a = new System.Data.Objects.ObjectContext(new EntityConnection());

            EntityCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SpatialDatabaseContext.MY_PROCEDURE";
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("ARG1", arg1);
            cmd.Parameters.AddWithValue("ARG2", arg2);

            EntityParameter resultParam = cmd.Parameters.Add("RESULT", DbType.String, 100);
            resultParam.Direction = ParameterDirection.Output;

            int c = cmd.ExecuteNonQuery();

            return (string)resultParam.Value;
        }
    }
}

这使我在类(class)名称下出现一条弯曲的蓝线,并显示错误消息。

我知道这段代码有效。这可以在其他地方运行。
那为什么这个副本会给我这个错误呢?

[编辑]

实际上,发生的情况是从数据库对象构建EDMX文件时添加了缺少的构造函数。这就是为什么它是部分类(class)!
我们每天都学到新东西!

[/编辑]

最佳答案

由于ObjectContext没有任何接受0参数的构造函数,并且您没有用SpatialDatabase类调用任何base(...)构造函数,因此您的代码是,隐式调用了基类的默认构造函数base()

您当前的代码等效于:

public partial class SpatialDatabase : global::System.Data.Objects.ObjectContext
{

       SpatialDatabase() : base() //Problem is here
       {
       }

}

您需要使用类构造函数调用以下基本构造函数之一
  • ObjectContext(EntityConnection)
  • ObjectContext(String)
  • ObjectContext(EntityConnection, String)
  • ObjectContext(String, String)
  • 关于c# - 'System.Data.Objects.ObjectContext'不包含带有0个参数的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11665629/

    相关文章:

    compiler-errors - 为什么 'dsgaudiko'有效的PHP?

    c++ - C++模板泛型(模板参数列表)

    c# - 排序列表>,错误: InvalidOperati

    apache-kafka - Kafka + Zookeeper : Connection to n

    c# - “does not contain a static '主要' method”错误

    node.js - 编译 Node 给出 “No-old-style-declaration”标志错

    objective-c - 架构armv7和Phonegap的 undefined symbol

    c# - 通用 - 编译器警告 CS0693 或错误

    boost - 使用复合键从 multi_index_container 中删除每个键的元素

    android - 从现有代码创建新项目