Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know the physical size of the database in Entity Framework?

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

like image 560
Dmytro Avatar asked Sep 05 '25 03:09

Dmytro


1 Answers

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]);
like image 91
Charlie Avatar answered Sep 07 '25 19:09

Charlie