Masriyah
Masriyah

Reputation: 2515

Connecting to database - Object doesn't contain constructor that takes 1 argument

I am trying to create a connection to the database and i am running into this error and not sure where to start or what to fix.

Error:
'object' does not contain a constructor that takes 1 argument

 public partial class DocMgmtDataContext
{
    public DocMgmtDataContext()
        : base(ConfigurationManager.ConnectionStrings["ProjectS"].ConnectionString)
    {
        OnCreated();
    }
}

In App.config

<configuration>
  <connectionStrings>
    <add name="ProjectS" connectionString="Data Source=.;Initial Catalog=OverallProg;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Upvotes: 0

Views: 2221

Answers (4)

Bhushan Firake
Bhushan Firake

Reputation: 9458

Inherit your class from DbContext. The constructor constructs a new context instance using the given string as the name or connection string for the database to which a connection will be made.

public partial class DocMgmtDataContext :DbContext
{
    public DocMgmtDataContext()
        : base(ConfigurationManager.ConnectionStrings["ProjectS"].ConnectionString)
    {
        OnCreated();
    }
}

Upvotes: 1

sachin
sachin

Reputation: 91

If you are using EF then check out the following steps that you have followed or not.

  1. Create library project => right click on project file => add new item => choose data section from left pane => select ADO.net Entity data model.

  2. it will popup with one window where you need to select generate from database option.(database first) and click next.

  3. in next window you have to select your database to which you want to connect with.

  4. EF automatically generates EDMX file which contains two parts one is context class and another is all classes(entities) for you.

  5. in context class you can find the constructor that you are talking about having connection string as parameter.

  6. now you need to just use this class library in your project and to access database you need to make context class object. and with this object you can do your all CRUD operations easily.

  7. These are just simple steps to connect to database. I may not have added every steps but I think these are also useful steps. Thanks !

Upvotes: 1

bloparod
bloparod

Reputation: 1726

Your class DocMgmtDataContext is inheriting from object, this is why you're getting the error 'object' does not contain a constructor that takes 1 argument.

Upvotes: 0

AlwaysAProgrammer
AlwaysAProgrammer

Reputation: 2919

The DataSource is of the form <ServerName>\<InstanceName>. The . refers to the local server. But you are missing the instance name

Upvotes: 0

Related Questions