Multiple foreign keys to the same table

后端 未结 4 403
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 16:54

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 tab

相关标签:
4条回答
  • 2021-02-04 17:32

    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.

    0 讨论(0)
  • 2021-02-04 17:39

    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.
      ...
    
    0 讨论(0)
  • 2021-02-04 17:51

    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.

    0 讨论(0)
  • 2021-02-04 17:57

    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.

    0 讨论(0)
提交回复
热议问题