2

是否可以在没有 DataContext 实例的情况下访问 Linq 到 SQL 映射数据?

我问是因为我正在编写一些只会触发某些实体和某些实体列的审计数据生成代码。我想在任何 Linq DB 访问之前在静态构造函数中修复这个元数据。

例如,从性能的角度来看,最好只发现一次实体的主键列,而不是为 ChangeSet 中的每个更改的实体触发以下代码:

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

打电话前:

key.GetValue(entity, null),
4

1 回答 1

2

是的,您不需要 的实例DataContext,只需要类型。

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

我在这里使用AttributeMappingSource,您可以使用XmlMappingSource或其他实现MappingSource

于 2009-11-18T14:37:30.600 回答