I've been using a DAL Class Library for a ASP.NET MVC Site. The DAL Library has a repository for the queries.
It was all working fine until I tried to use the DAL library for a windows service, the service throws a exception when I reach the RARepository _repository = new RARepository();
or more specifically when the repository hits the private readonly RAEntities6 entities = new RAEntities6();
line.
The Exception thrown is
System.ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
at RA.Models.RAEntities6..ctor() in C:\RADAL\Models\RAEntities.Designer.cs:line 54
at RA.Repositories.RARepository..ctor() in C:\RADAL\Repositories\RARepository.cs:line 11
at GDMService.ASU.OnElapsedTime(Object sender, ElapsedEventArgs e) in C:\RAPC7\GDMService\Service1.cs:line 78
I thought it may be a issue with the connection string using integrated security from a service (which runs as local sytem) so i tried first changing the connection string in app.config to have a user name and password for the sql server. The repositories still worked for the MVC Site, but not for the Service. After that i tried changing the connection string back to integrated security and made the service run as a user but the error was the same.
In the exception the line 78 is RARepository _repository = new RARepository();
, the line 11 is private readonly RAEntities6 entities = new RAEntities6();
and the line 54 is in the entity framework designer constructor
public RAEntities6() : base("name=RAEntities6", "RAEntities6")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
Edit: I think what has happened is that when the service is running it no longer has permissions to open the config file (which contains the connection string), I have found now that when the service is running a load of .INI files can not be found but I'm not sure how to resolve this to give the service permissions, or where to put the config files so the service can access everything.