没有 DataContext 的 Linq to SQL 映射数据

发布于 2024-08-12 02:16:26 字数 470 浏览 2 评论 0原文

是否可以在没有 DataContext 实例的情况下访问 Linq to 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),

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),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2024-08-19 02:16:26

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

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

这里我使用 AttributeMappingSource,您可以使用 XmlMappingSourceMappingSource 的其他实现。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文