1

We can get table name if we have table type. like here

DataContext dc = new DataContext();
var tableName = dc.Mapping.GetTable(type).TableName.ToString();

In my case I have table name and wants to get table type. but how?

Note: Table name and type are different (in spelling) in my DataContext case. So i can't convert table name to type like below.

Type tableType = Type.GetType("dbo." + tableName);
rana
  • 145
  • 1
  • 2
  • 8
  • I don't even know how you got the table name to be different from the data type. Didn't know that was possible – DJ Burb Jan 22 '13 at 15:54
  • `DataContext` take singular table type. Like if you have table name `Companies` then `DataContext` take `Company` table type. So both are different in spelling. – rana Jan 23 '13 at 11:52
  • oh yeah okay. I know what you are talking about – DJ Burb Jan 23 '13 at 16:26

2 Answers2

0

I assume you found a solution, but if not please have me elaborate.

I just wanted to note that I ended up using DbContext (http://msdn.microsoft.com/en-us/data/jj729737.aspx) instead of DataContext. I tried creating a derived class to basically rename it, but that didn't work. DataContext needed to have its hand held with all the attribute metadata ([Column], for example). If I bothered to do this I might as well re-code the models (which is bad, since it's code duplication).

In my case, my database was being generated/managed by an ASP.NET Web API project. I shared my models and database with a separate (and eventually independent) class library. This can cause some problems so make sure to disable model compatibility checking: How can I disable model compatibility checking in Entity Framework 4.3?.

Community
  • 1
  • 1
brudert
  • 537
  • 8
  • 21
0
    var db = new DataContext();
    var tables = db.Mapping.GetTables();
    var tableTypeName = tables.Where(r => r.TableName == "yourTableName").Select(r => r.RowType.Name).FirstOrDefault();
Eugene
  • 1