Reputation: 183
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
Reputation: 195
This process worked for me
public DataClasses1DataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["juegosConnectionString"].ConnectionString,mappingSource){}
DataClasses1DataContext data = new DataClasses1DataContext();
Upvotes: -1
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
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 class
DataContext` your code should compile (and work) just fine.
Upvotes: 0