Ramon Johannessen
Ramon Johannessen

Reputation: 183

DBML DataClasses constructor: 'object' does not contain a constructor that takes 1 arguments

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?

Upvotes: 0

Views: 988

Answers (3)

This process worked for me

 public DataClasses1DataContext() : 
    base(global::System.Configuration.ConfigurationManager.ConnectionStrings["juegosConnectionString"].ConnectionString,mappingSource){}


  DataClasses1DataContext data = new DataClasses1DataContext();

enter image description here

Upvotes: -1

Wasim Akram
Wasim Akram

Reputation: 1

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.

Upvotes: -1

Jens Meinecke
Jens Meinecke

Reputation: 2940

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.

Upvotes: 0

Related Questions