Is it possible to know code first created database physical *.mdf
file size in bytes, using only methods or properties of Entity Framework classes? And if it's possible, then how to do that???
Asked
Active
Viewed 3,472 times
2
-
I googled only some tricky SQL queries to do that by calculating and summarizing of all entries size, but I suggest there is some easier, safer and quicker way. – Dmytro Aug 20 '12 at 01:39
-
1There isn't. EF is a database independent ORM. What if the database is split into multiple files? Your use case is a frequently occuring scenario. – Eranga Aug 20 '12 at 02:07
1 Answers
9
You can get the database size by exec the sp_spaceused
var ef = new EFContext();
var sqlConn = ef.Database.Connection as SqlConnection;
var cmd = new SqlCommand("sp_spaceused")
{
CommandType = System.Data.CommandType.StoredProcedure,
Connection = sqlConn as SqlConnection
};
var adp = new SqlDataAdapter(cmd);
var dataset = new DataSet();
sqlConn.Open();
adp.Fill(dataset);
sqlConn.Close();
//You will get:
//database_name database_size unallocated space
//performance 1091.00 MB 456.00 MB
//
//reserved data index_size unused
//557056 KB 540680 KB 10472 KB 5904 KB
Console.WriteLine(dataset.Tables[0].Rows[0][1]);

Charlie
- 1,292
- 8
- 7