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?
SiteContext's constructor?