views:

104

answers:

1

Is it possible to access Linq to SQL mapping data without a DataContext instance?

I ask because I am writing some audit data generation code that will only trigger for some entities and some entity columns. I would like to fix up this meta data in a static constructor prior to any Linq DB access.

For example from a performance perspective it would be preferable to discover the primary key column of an entity just once instead of triggering the following code for each changed entity in the ChangeSet:

var metaTable = context.Mapping.GetTable(entityType);
var key = (PropertyInfo)metaTable.RowType.DataMembers.Single(
                   md => md.IsPrimaryKey).Member;

Prior to calling:

key.GetValue(entity, null),
A: 

Yes, you don't need an instance of DataContext, only the type.

MappingSource mappingSource = new AttributeMappingSource();
MetaModel mapping = mappingSource.GetModel(typeof(MyDataContext));

Here I'm using AttributeMappingSource, you could use XmlMappingSource or other implementations of MappingSource.

Max Toro
Excellent news. Since I just dragged table names onto the Visual Studio Linq model window I assume an AttributeMappingSource mapping source is all I need?
camelCase
Yes, the VS designer uses attributes.
Max Toro