In a console application, I have a SQL Server CE database which I am accessing using Entity Framework. Why is the hash value of the .sdf
database changing after a read only operation?
class Program
{
static void Main(string[] args)
{
string fileName = @" C:\...\bin\Debug\exportedDb.sdf";
Console.WriteLine("The Hash Before is -->"+ComputeHash(fileName));
using (var context = new DbaseEntities())
{
Console.WriteLine(context.ANNEES.First().ANNEE_UNIVERSITAIRE);
}
Console.WriteLine("The Hash After is -->" + ComputeHash(fileName));
Console.ReadLine();
}
public static string ComputeHash(string fileName)
{
var hash = "";
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(fileName))
{
hash = BitConverter.ToString(md5.ComputeHash(stream));
}
return hash;
}
}
}
The above code gives returns these results :
When the SQL CE engine does sorting and other internal operations, they can be written to temporary tables/objects in the database file.
BUT, you can prevent this from happening by changing your connection string. Add Mode = Read Only to your connection string.
Data Source=C:\data\mydb.sdf;Mode=Read Only