同一个表的多个外键
我有一个参考表,其中包含性别、地址类型、联系人类型等的各种受控值查找数据。许多表对此参考表有多个外键。
我还有多对多关联表,其中有两个外键同一张桌子。不幸的是,当这些表被拉入 Linq 模型并生成 DBML 时,SQLMetal 不会查看外键列的名称或约束的名称,而只会查看目标表。所以我最终得到了名为 Reference1、Reference2 的成员,...不太便于维护。示例:
<Association Name="tb_reference_tb_account" Member="tb_reference" <======
ThisKey="shipping_preference_type_id" OtherKey="id" Type="tb_reference"
IsForeignKey="true" />
<Association Name="tb_reference_tb_account1" Member="tb_reference1" <======
ThisKey="status_type_id" OtherKey="id" Type="tb_reference"
IsForeignKey="true" />
当然,我可以进入 DBML 并手动更改成员名称,但这意味着我无法再往返我的数据库模式。在该模型的现阶段,这不是一个选项,该模型仍在不断发展。 将参考表拆分为 n 个单独的表也是不可取的。
我可能可以编写一个脚本,在每次生成后针对 XML 运行,并将成员名称替换为从 ThisKey 派生的名称(因为我遵守这些类型的键的命名约定)。 有没有人找到更好的解决方案来解决这个问题?
I have a reference table with all sorts of controlled value lookup data for gender, address type, contact type, etc. Many tables have multiple foreign keys to this reference table
I also have many-to-many association tables that have two foreign keys to the same table. Unfortunately, when these tables are pulled into a Linq model and the DBML is generated, SQLMetal does not look at the names of the foreign key columns, or the names of the constraints, but only at the target table. So I end up with members called Reference1, Reference2, ... not very maintenance-friendly. Example:
<Association Name="tb_reference_tb_account" Member="tb_reference" <======
ThisKey="shipping_preference_type_id" OtherKey="id" Type="tb_reference"
IsForeignKey="true" />
<Association Name="tb_reference_tb_account1" Member="tb_reference1" <======
ThisKey="status_type_id" OtherKey="id" Type="tb_reference"
IsForeignKey="true" />
I can go into the DBML and manually change the member names, of course, but this would mean I can no longer round-trip my database schema. This is not an option at the current stage of the model, which is still evolving.
Splitting the reference table into n individual tables is also not desirable.
I can probably write a script that runs against the XML after each generation and replaces the member name with something derived from ThisKey (since I adhere to a naming convention for these types of keys).
Has anybody found a better solution to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所以我走上了部分课程的路线。例如,我添加了以下成员来解决原始示例中的第一个引用成员:
这远非完美。在大型模型中它需要大量的额外代码,并且依赖于属性的顺序不改变(如果引用表的另一个外键被添加到帐户表中,则该成员实际上可能指向除运输偏好)。
这也有一个好处。由于我已经为其他目的编写了部分类,因此添加这些成员不需要我重新构建应用程序。
So I went down the partial classes route. For instance, I added the following member to address the first reference member in my original example:
This is far from perfect. It requires a substantial amount of extra code in a large model, and depends on the order of the attributes to not change (if another foreign key to the reference table is added to the account table, this member may actually point to something else than the shipping preference).
There is an upside, too. Since I am already writing partial classes for other purposes, adding these members did not require that I rearchitect the application.
在 VS2010 中,您实际上可以在视图中重命名父级和子级的属性。虽然它有点隐藏。
更多详细信息可以在此处找到:
http://weblogs.asp.net/scottgu/archive/2007/05/29/linq-to-sql-part-2-defining-our-data-model-classes.aspx
其中是我从哪里得到它的。
In VS2010 You can actually rename the properties for parent and child in the view. It is a bit hidden though.
More details can be found here:
http://weblogs.asp.net/scottgu/archive/2007/05/29/linq-to-sql-part-2-defining-our-data-model-classes.aspx
Which is where I got it from.
当前用于 LINQ 的 MS 工具有些有限,而且看起来相对于 2010 来说并没有太多的努力。您可以编写自己的代码生成器,请参阅 Damien 的 t4 模板 或 PLINQO。我还发现 EDMDesigner 可能值得一看。
The current ms tooling for LINQ is some what limited and it doesn't really look like much effort will be coming for vs 2010. You can write your own code generator, look at Damien's t4 templates or PLINQO. I have also found EDMDesigner which might be worth a look.
我的 sqlmetal 的 xml 有更合理的默认值。不完美,但更好。
第一个 FK 获取表名,第二个 FK 获取字段名。
sqlmetal版本:
my xml from sqlmetal had much more sensible defaults. not perfect, but better.
The first FK got the table name, but the second got the field name.
sqlmetal version: