Dmytro
Dmytro

Reputation: 17176

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

Upvotes: 4

Views: 3958

Answers (1)

Charlie
Charlie

Reputation: 1332

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]);

Upvotes: 11

Related Questions