1

LINQ to SQLを使用してプロジェクトを開始したばかりで、多対多の問題と衝突しました。私は多くの可能な解決策を見つけました、そして私はこれを選びまし

とにかく、影響を受けるdbテーブルは次のようになります。

Customer (idcustomer,other stuff..)
Customer_Role(idcustomer, idrole, other_attribute_I_want_to_keep )
Role(idrole, other stuff)

では、「other_attribute_I_want_to_keep」をどのように処理できますか?次のようにすると便利です。

顧客c.Role.other_attribute_I_want_to_keepですが、考えられる解決策を見つけることができません。

ありがとう

4

1 に答える 1

2

これには多対多を使用することはできません。BelongsToむしろ、CustomerRoleをCustomerとRoleの両方を持つファーストクラスのオブジェクトにする必要があります。HasOne次に、CustomerとRoleは、それぞれCustomerRoleの属性を宣言する必要があります。次に、次のことができます。

c.CustomerRole.Role
c.CustomerRole.OtherAttribute

次のように定義されたショートカットRole属性を作成できます。Customer

[NotMapped]
public Role Role { 
    get { return CustomerRole == null ? (Role)null : CustomerRole.Role; } 
    set { if (CustomerRole == null)
            CustomerRole = new CustomerRole(){ Role = value };
          else
            CustomerRole.Role = value;
    }
}

ただし、複雑なクエリを作成するときにこのショートカットを使用すると問題が発生する可能性があることに注意してください。

于 2012-08-31T15:45:48.753 に答える