I am new to Entity Framework, after a crash course and some playing around my employer decided to use ef 5, code first.
I have separated the dbcontext derivative along with the repositories and connection strings in a assembly on its own. The idea is to create an assembly which can be reusable across projects.
The connection string in the database assembly looks like this:
<add name="LmsDb" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=LmsDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\LmsDb.mdf;;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
While the connection string on assemblies using the database project (the clients) looks like this:
<add name="LmsDb" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=LmsDb;Integrated Security=SSPI;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
I have created a MVC project which works just fine with this and all my integration tests works well with this connection string.
The problem occurred when I had to create some kind of scheduler logic to go through the database at some configurable intervals and do some actions according to the data there. I decided to test this out by creating a simple Windows Service with a Timer that executes the ef 5 code for some queries.
The timer is set to execute a Select all query every minute, but when this happends my query returns no results although I know that there is data in the database.
When I look at the DbContext.Connection.State, its set to Closed.
What am I doing wrong ?
I tried opening the connection explicit by using ((ObjectContextAdapter)my context).ObjectContext.Connection.Open() according to Julie Lerman.
This didn’t seem to help any though.
This got resolved. Please look at my comment.