title: EF6 with SQL Server CE 4.0, Code-First, check for valid Database-File
body: <p>I´m using EF6 with an SQL Server Compact database, Code-First approach and MigrateToLatestVersion-Initializers. I also have two DbContext-derived classes which use the same database. Programming language is C#, IDE is Visual Studio 2013.</p>
  
  <p>On application startup, I want to check wether a given database-file is valid or not. First I check if the file exists, but then I also would like to check if the file is a valid database. </p>
  
  <p>My first approach was to create an instance of each of my DbContext-classes. But as this doesn´t access the Database, I can´t catch any exceptions that would imply an invalid file.</p>
  
  <p>But also when i call a function that accesses the Database, like <code>SomeContextEntities.ToList()</code>, I Don´t get any exceptions, even if the file I check is a simple text-document. I guess the Initializer simply overwrites the invalid text-file and creates a new database. </p>
  
  <p>Then I tried <code>SomeContext.Database.Connection.Open()</code>, hoping that method would throw an exception when the database-file is invalid, but it doesn´t and the ConnectionState simply changes to "open". As a matter of fact, when I call <code>SomeContext.Database.Connection.Close()</code>, the formerly text-file has been transformed into an empty database-file.</p>
  
  <p>Also <code>SomeContext.Database.Exists()</code> apparently just checks for file-existance.</p>
  
  <p>Is there a (preferably low-cost) way to check for a valid database-file?</p>