LINQ to SQL - 动态表属性
我目前在一家支付提供商工作。我想从 Provider 配置中获取事务表名称以在 LINQ 查询中使用,但了解以下示例是不可能的:
[Table(Name = Config.TransactionTable)]
public class Transaction : ITransaction
{
....
}
当我执行上述操作时,我在编译时收到以下错误:
属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式
解决此问题的最佳方法是什么?是否使用 LINQ to SQL 来实现我想要的目标?
I am currently working on a payment provider. I want to get the transaction table name from the Provider configuration for use in my LINQ queries but understand that the following example is not possible:
[Table(Name = Config.TransactionTable)]
public class Transaction : ITransaction
{
....
}
I get the following error at compile time when I do the above:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
What would be the best approach to this problem and is using LINQ to SQL to acheive what I want out of the question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
属性 (
TableAttribute
) 是sealed
,这限制了许多选项;但是,LINQ-to-SQL 并不依赖于属性模型 - 还有其他提供程序。最值得注意的是,您可能会考虑使用 xml 模型提供程序,并在运行时生成 xml。这是重载构造函数中的MappingSource
参数;典型的用法是XmlMappingSource.FromXml(...)
或类似的。然而,核心表在运行时发生变化的情况通常看起来相当罕见。不常见,但并非不受支持。
作为参考,隐式默认值为
AttributeMappingSource
。The attribute (
TableAttribute
) issealed
, which limits many options; however, LINQ-to-SQL is not tied to the attribute model - there are other providers. Most notably, you might look at using the xml model provider, and generating the xml at runtime. This is theMappingSource
parameter in the overloaded constructor; a typical usage would beXmlMappingSource.FromXml(...)
or similar.However, it generally seems pretty uncommon that the core tables will change at runtime. Uncommon, but not unsupported.
For reference, the implicit default is
AttributeMappingSource
.