2

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???

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dmytro
  • 16,668
  • 27
  • 80
  • 130
  • 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
  • 1
    There 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 Answers1

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