0

I have inherited some code (that happens a lot!) which looks a bit like this: (namespace omitted)

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;
        }
    }
}

This gives me a sqiggly blue line under my class name with the error message.

I know that this code works. This is running elsewhere on site just fine. So why would this copy give me this error?

[Edit]

In reality, what happens is that the missing constructor is added when the EDMX file is built from the database objects. That's why it's a partial class! We learn something new every day!

[/Edit]

CompanyDroneFromSector7G
  • 4,291
  • 13
  • 54
  • 97

2 Answers2

2

Since ObjectContext doesn't have any constructor that takes 0 arguments and you haven't called any base(...) constructor with your SpatialDatabase class, your code is implicitly calling default constructor base() for the base class.

Your current code is equivalent to:

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

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

}

You need to call one of the following base constructor with your class constructor

Habib
  • 219,104
  • 29
  • 407
  • 436
  • So does this mean that the code as it stands will not work without modification? This comes from a third party and is allegedly 'working'. – CompanyDroneFromSector7G Jul 26 '12 at 09:01
  • @bukko, may be you are missing a file , which hold the remaining partial class SpatialDatabase, can't be sure though. But yes in current state it shouldn't compile – Habib Jul 26 '12 at 09:02
1

It is a warning saying that your derived class isn't providing a constructor with parameters, which you might want to pass on to the inherited class' constructor. This might potentially cause a problem.

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95