开发者

How to get List of all tables in the Entity data framework?

开发者 https://www.devze.com 2023-02-01 11:50 出处:网络
I need to get the list of All tables in the Entity Data Framework. I know that in Linq2SQL we can use something like this.

I need to get the list of All tables in the Entity Data Framework.

I know that in Linq2SQL we can use something like this. var dataContext = new DataContext(); var dataContextTableNames = (from tables in dataContext.Mapping.GetTables() select tables.TableName).ToList();

But, I need to get list of all tables in Entity Data Framework. There is any 开发者_开发百科work around to get similar list in Entity Data Framework.

Thanks in advance.


[Edit]

Perhaps this can be of use to find the number of objects in Storage space

   var count = GetEntitySetCount(myObjectContext.MetadataWorkspace);

   public static int GetEntitySetCount(MetadataWorkspace workspace)
   {
        var count = 0;

        // Get a collection of the entity containers from storage space.
        var containers = workspace.GetItems<EntityContainer>(DataSpace.SSpace);

        foreach(var container in containers)
        {
            //Console.WriteLine("EntityContainer Name: {0} ",
            //                  container.Name);

            foreach(var baseSet in container.BaseEntitySets)
            {
                if(baseSet is EntitySet)
                {
                    count++;

                    //Console.WriteLine(
                    //    "  EntitySet Name: {0} , EntityType Name: {1} ",
                    //    baseSet.Name, baseSet.ElementType.FullName);
                }
            }
        }

        return count;
    }

To retrieve the number of tables in the database, you can do the following in .Net 4.0

myObjectContext.ExecuteStoreQuery<int>(
                "SELECT COUNT(*) from information_schema.tables WHERE table_type = 'base table'");

Using .Net 3.5

var connection = ((EntityConnection)myObjectContext.Connection).StoreConnection as SqlConnection;

var cmd = new SqlCommand("SELECT COUNT(*) from information_schema.tables WHERE table_type = 'base table'", connection);

connection.Open();

var count = (int)cmd.ExecuteScalar();

connection.Close();
0

精彩评论

暂无评论...
验证码 换一张
取 消