I created LINQ to SQL file which represent my database. The table names in the database are formated like so:
我创建了代表我的数据库的LINQ to SQL文件。数据库中的表名称格式如下:
[prefix]_tableName
and I want the prefix to be dynamic, when I tried to change the TableAttribute in the designer.cs file from:
当我尝试更改designer.cs文件中的TableAttribute时,我希望前缀是动态的:
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.isftblprfx_config")]
to:
[global::System.Data.Linq.Mapping.TableAttribute(Name=String.Format("dbo.{0}_{1}", Server.TablesPrefix, "config")]
I got build time error:
我有构建时间错误:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式
There is any solution? Thanks in advance
有什么解决方案吗?提前致谢
2 个解决方案
#1
1
The exception says it all.
这个例外说明了一切。
An attribute is emitted into metadata when you compile your code, so its arguments need to be determined at compile time, they can not be changed at runtime. So I expect that your Server.TablesPrefix
is not a constant or readonly string.
编译代码时会将属性发送到元数据中,因此需要在编译时确定其参数,并且不能在运行时更改它们。所以我希望您的Server.TablesPrefix不是常量或只读字符串。
If you want to reuse your context for different databases they should have identical schemas. If you can't change the schemas, maybe t4 templates offer a solution.
如果要将上下文重用于不同的数据库,则它们应具有相同的模式。如果您无法更改模式,可能t4模板提供了解决方案。
#2
0
[global::System.Data.Linq.Mapping.TableAttribute(Name=String.Fromat("dbo.{0}_{1}", Server.TablesPerfix, "config")]
should read
[global::System.Data.Linq.Mapping.TableAttribute(Name=String.Format("dbo.{0}_{1}", Server.TablesPrefix, "config")]
Typo's are always the worst enemy :)
错字总是最大的敌人:)
#1
1
The exception says it all.
这个例外说明了一切。
An attribute is emitted into metadata when you compile your code, so its arguments need to be determined at compile time, they can not be changed at runtime. So I expect that your Server.TablesPrefix
is not a constant or readonly string.
编译代码时会将属性发送到元数据中,因此需要在编译时确定其参数,并且不能在运行时更改它们。所以我希望您的Server.TablesPrefix不是常量或只读字符串。
If you want to reuse your context for different databases they should have identical schemas. If you can't change the schemas, maybe t4 templates offer a solution.
如果要将上下文重用于不同的数据库,则它们应具有相同的模式。如果您无法更改模式,可能t4模板提供了解决方案。
#2
0
[global::System.Data.Linq.Mapping.TableAttribute(Name=String.Fromat("dbo.{0}_{1}", Server.TablesPerfix, "config")]
should read
[global::System.Data.Linq.Mapping.TableAttribute(Name=String.Format("dbo.{0}_{1}", Server.TablesPrefix, "config")]
Typo's are always the worst enemy :)
错字总是最大的敌人:)