1

I'm using SQL Server 2014 LocalDb with C#. One issue is that if the application is inactive for a certain period of time, the database expires and the next action takes a long time while it re-initializes the database. What is the best way to prevent such timeout?

I'm accessing the database via Linq to Entities.

One approach I tried is calling this function every 60 seconds, but even that doesn't seem to be working properly for some reason.

public async static Task<Version> GetVersionInfoAsync() {
    try {
        return await Task.Run(() => {
            using (Entities context = new Entities()) {
                // We must query manually because querying entity objects would fail on outdated databases.
                DbVersion Result = context.Database.SqlQuery<DbVersion>("SELECT Major, Minor, Build, Revision FROM Version").FirstOrDefault();
                //DbVersion Result = context.DbVersions.Single();
                return new Version(Result.Major, Result.Minor, Result.Build, Result.Revision);
            }
        });
    }
    catch (Exception ex) {
        throw ex;
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Etienne Charland
  • 3,424
  • 5
  • 28
  • 58
  • http://stackoverflow.com/questions/14153509/how-to-prevent-sql-server-localdb-auto-shutdown – artm Apr 07 '15 at 04:35
  • There are 2 drawbacks to this. First, the instance has to be reset for this to take effect which I have no control over. Second, I want the database to keep running for as long as the application is running. Sometimes the application could be left running other stuff for 30 minutes or over an hour, so even setting the timeout to an hour wouldn't be the best solution. And if I set it too high, it will keep using necessary resources for a long time after closing the app. – Etienne Charland Apr 08 '15 at 16:49
  • the instance doesn't have to be reset to take effect. after executing the script mentioned in the URL in @artm's comment, you need to issue another RECONFIGURE statement. I do this and don't need to restart the instance. – Richard II Dec 11 '17 at 22:55
  • I ended up switching to SQLite and everything is working smoothly – Etienne Charland Dec 12 '17 at 17:45

0 Answers0