同一个表的多个外键

发布于 2024-08-05 10:28:24 字数 795 浏览 3 评论 0原文

我有一个参考表,其中包含性别、地址类型、联系人类型等的各种受控值查找数据。许多表对此参考表有多个外键。

我还有多对多关联表,其中有两个外键同一张桌子。不幸的是,当这些表被拉入 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 技术交流群。

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

发布评论

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

评论(4

走过海棠暮 2024-08-12 10:28:24

所以我走上了部分课程的路线。例如,我添加了以下成员来解决原始示例中的第一个引用成员:

public partial class tb_account
{
    public tb_reference shipping_preference_reference
    {
        get
        {
            return this._tb_reference.Entity;
        }
        set
        {
            this.tb_reference = value;
        }
    }

这远非完美。在大型模型中它需要大量的额外代码,并且依赖于属性的顺序不改变(如果引用表的另一个外键被添加到帐户表中,则该成员实际上可能指向除运输偏好)。
这也有一个好处。由于我已经为其他目的编写了部分类,因此添加这些成员不需要我重新构建应用程序。

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:

public partial class tb_account
{
    public tb_reference shipping_preference_reference
    {
        get
        {
            return this._tb_reference.Entity;
        }
        set
        {
            this.tb_reference = value;
        }
    }

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.

与他有关 2024-08-12 10:28:24

在 VS2010 中,您实际上可以在视图中重命名父级和子级的属性。虽然它有点隐藏。

  1. 在 dbml 查看器中选择困扰您的关系。
  2. 在“属性”网格中,您将有两行“子属性”和“父属性”。
  3. 展开它们,您可以更改属性的名称

更多详细信息可以在此处找到:
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.

  1. In the dbml viewer select the relationship that bothers you.
  2. In the Properties grid you will have two rows Child and Parent Property.
  3. Expand them there you can change the Name of the property

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.

赠意 2024-08-12 10:28:24

当前用于 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.

把人绕傻吧 2024-08-12 10:28:24

我的 sqlmetal 的 xml 有更合理的默认值。不完美,但更好。
第一个 FK 获取表名,第二个 FK 获取字段名。

  <Association Name="FK_Product_DefaultOutputTypeID" Member="OutputType"
   ThisKey="DefaultOutputTypeID" OtherKey="OutputTypeID" Type="OutputType"
   IsForeignKey="true" />
  <Association Name="FK_Product_DefaultTileOutputTypeID" Member="DefaultTileOutputType"
   ThisKey="DefaultTileOutputTypeID" OtherKey="OutputTypeID" Type="OutputType"
   IsForeignKey="true" />

sqlmetal版本:

  C:\Program Files\Microsoft Visual Studio 9.0\VC>sqlmetal /?
  Microsoft (R) Database Mapping Generator 2008 version 1.00.21022
  for Microsoft (R) .NET Framework version 3.5
  Copyright (C) Microsoft Corporation. All rights reserved.
  ...

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.

  <Association Name="FK_Product_DefaultOutputTypeID" Member="OutputType"
   ThisKey="DefaultOutputTypeID" OtherKey="OutputTypeID" Type="OutputType"
   IsForeignKey="true" />
  <Association Name="FK_Product_DefaultTileOutputTypeID" Member="DefaultTileOutputType"
   ThisKey="DefaultTileOutputTypeID" OtherKey="OutputTypeID" Type="OutputType"
   IsForeignKey="true" />

sqlmetal version:

  C:\Program Files\Microsoft Visual Studio 9.0\VC>sqlmetal /?
  Microsoft (R) Database Mapping Generator 2008 version 1.00.21022
  for Microsoft (R) .NET Framework version 3.5
  Copyright (C) Microsoft Corporation. All rights reserved.
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文