0

I've found a million examples of how this is supposed to work, but I can't figure out why it doesn't recognize the class properly, as if it weren't marked "partial". Here's my partial class to allow me to define my DB connection string in a config file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration; // For Reading Connection String from Web.Config

namespace PSDataClasses
{
    public partial class DataClassesDataContext
    {
        public DataClassesDataContext() : base(ConfigurationManager.ConnectionStrings["PSDataClasses.Properties.Settings.DBConnectionString"].ConnectionString)
        {
            OnCreated();
        }
    }
}

But OnCreated() does not exist in the current context, and it thinks my class is an object...what am I missing?

2 Answers2

0

You have merely defined class DataClassesDataContext without inheriting from another class (unless you have done that in another file...) therefore DataClassesDataContext is inheriting from object - all classes derive from object at the top of the hierarchy.

You are then calling the base constructor with a single argument (what appears to be a string). But object does not have a constructor that takes a string as an argument - as a matter of fact it only has one constructor, which is parameterless. Hence you get the error - you are trying to call a constructor that does not exist.

I think you are missing a class inheritance declaration to say it is inherited from (say) DataContext:

public partial class DataClassesDataContext : DataContext
{
    public DataClassesDataContext() : base(ConfigurationManager.ConnectionStrings["PSDataClasses.Properties.Settings.DBConnectionString"].ConnectionString)
    {
        OnCreated();
    }
}

Now calling base(....) will invoke the constructor for 'DataContextwith a string as a parameter. And if such a constructor exists for classDataContext` your code should compile (and work) just fine.

Jens Meinecke
  • 2,904
  • 17
  • 20
  • So I wasn't actually misunderstanding the partial class. After I initially accepted your answer I realized I had the class in a different namespace than the DataClasses.designer.cs partial DataClassesDataContext, so it wasn't actually a partial class of the type of that class, it was an object class obviously. Thanks for getting me thinking ;) – Ramon Johannessen Apr 29 '17 at 05:11
0

I have also faced this issue. Actually, my problem was that I was not inheriting my context class from DbContext. After fixing that it worked fine.

dstrants
  • 7,423
  • 2
  • 19
  • 27