0

I am developing an application that is differentiating between an existing database and a backup-file (.bak). When generally connecting and retrieving data from SQL Server, I prefer to use Linq, since I find it easier to use this when building queries.

In this case I do not see how I can do this, my procedure is that I construct a string with the SQL query for restoring the database (from the .bak file) and then retrieve whatever data that differs from the current one, with this one.

All queries that I use are strings which I format to add the database-name and schema. The strings that I am using become quite large and I find that it is very cluttering (I like clean code).

The structure of the databases are all the same and do not ever change between the different backups or the real deal. New backups come each day, and have to be checked against, so it isn't really an option to go in and add a new connection-file each time there is a new backup.

Is there any way that I can restore the database, using Linq, and then retrieve the data using Linq queries? Or am I doing it as I should?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sander
  • 1,264
  • 1
  • 16
  • 26

1 Answers1

0

You can only use Linq for queries so you can not use it to restore a database.

Also, you can't use different databases in the same Linq query, you can however materialize the results of each query to the database and make a third Linq query based on them, as you can see in this answer.

Community
  • 1
  • 1
jnovo
  • 5,659
  • 2
  • 38
  • 56
  • So if I am able to create the database using a normal query, can I then use Linq to get the data? – Sander Jan 31 '14 at 11:46
  • Yeah, but you'll need a context associated to the backup database. You can either create two, one for each database and then dynamically perform the restore (thus, changing the contents of the corresponding restored database), or maybe you can somehow reuse the context by simply pointing to your backup database (since the schema is the same). I'm unsure if you can simply accomplish the latter by using a different connection string pointing to the restore database. – jnovo Jan 31 '14 at 11:56