Linq to SQL mapping data without a DataContext
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),
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
.
精彩评论