views:

377

answers:

1

Hi,

With FileInfo I can find the file name of a DataContext .dbml file.

Provided I declare:

Dim DataModel = New AttributeMappingSource().GetModel(GetType(NorthwindDataContext))

With System.Data.LINQ.Mapping I can find the name of all Tables and furthermore their Columns and relationships.

All this thanks to the excellent post from Jomo Fisher here: LINQ to SQL Trick: Get all Table [and Column] Names: http://blogs.msdn.com/jomo_fisher/archive/2007/07/30/linq-to-sql-trick-get-all-table-names.aspx

But how can I achieve same result without explicitly knowing the DataContext Object? I mean how can i "replace" this:

            GetType(NorthwindDataContext))

With:

            dim myDCFile as String = "Northwind.dbml"
            Dim DataModel = .../... GetType(myDCFile))
A: 

You could try something like this:

Assembly dataAssembly = Assembly.Load("Your.Data.Assembly");

Type dataContextType = dataAssembly.GetTypes()
           .FirstOrDefault(x => x.IsSubclassOf(typeof(DataContext)));

You would basically load your data assembly and search for the first data type that is a subtype of DataContext.

You could then pass that into your method:

Dim DataModel = New AttributeMappingSource().GetModel(dataContextType)

Marc

marc_s
Thanks Marc... But... Have you tested the code snippet?As I'm more of a VB guy, I got your code translated: Dim dataAssembly As Assembly = Assembly.Load("Your.Data.Assembly") Dim dataContextType As Type = dataAssembly.GetTypes().FirstOrDefault(Function(x) x.IsSubclassOf(Type.GetType(DataContext)))As you can see (typeof(DataContext)" has been converted to "(Type.GetType(DataContext)"The hell if I know why!!! But the issue now is VB claims aout the (DataContext) part, saying "DataContext is a Type and cannot be used as an expression".Still struck... Sadly!
And if I reinstate TypeOf then at the end of expressin VB says: "Is expected"...
I tested it as C# code, yes. I'm not fluent enough in VB to know what's going on here with the translation, sorry.
marc_s