0

I'm passing the Connection String to my Entity Classes via parameter in the constructor, like this:

public class Site : ISite
{
    private readonly string _connectionString;

    public Site(string connectionString)
    {
        _connectionString = connectionString;
    }

An Exception is thrown when I query the Database like this:

var site = db.Sites.FirstOrDefault(s => s.Name.Equals(name));

The Exception:

InvalidOperationException: The class 'AuctionSite.Site' has no parameterless constructor.

According to the post I found on this thread the documentation states that:

There must be a parameterless constructor

If I create a parameterless constructor the Connection String won't be set and when I create a context inside a method, for example:

public IEnumerable<IUser> GetUsers()
{
    using (var db = new SiteContext(_connectionString))
    {
        return db.Users;
    }
}

_connectionString will be null and will fail when calling base constructor of DbContext.

How should I pass the Connection String to my Entity Classes if not through the constructor?

Is there a better way to do this? Maybe by materializing the Entity with my connection string as a parameter or with a better query?

1
  • The class 'AuctionSite.Site' has no parameterless constructor. What does that have to do with SiteContext's constructor? Commented Jan 22, 2019 at 22:51

2 Answers 2

0

It looks like you might be confusing Site the entity and SiteContext the DbContext. Site the entity needs the to have a parameterless constructor. SiteContext is the thing which deals with the connection string. The entities do not need to know about the connection string, in fact, they shouldn't know they are related to the database at all.

Additionally the code you have

public IEnumerable<IUser> GetUsers()
{
    using (var db = new SiteContext(_connectionString))
    {
        return db.Users;
    }
}

Will set the connection string just fine. The constructor which matches the arguments, in this case a string, is the one which is called.

Sign up to request clarification or add additional context in comments.

2 Comments

This is a University project and my Entities are implementations of given Interfaces so I'm not confusing the 2. Although what you say is usually true in this case my Entities do know about the connection string and have to implement queries like the one you quoted.
University or not having entities know about their database breaks SOLID principles (single responsibility) and shouldn't be done. I'd encourage you to go back to your professor and ensure that you've got the right approach.
0

There is nothing that stops you from having two constructors:

 public Site(string connectionString)
 {
     _connectionString = connectionString;
 }

 public Site()
 {
 }

2 Comments

True, but if I call GetUsers() on the Site that was created with the empty constructor my connection string will be null
Standard practice is to have the connection string stored in the web.config file so that your Site class can read the value from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.