2

I have a Code First MVC 4 Web app - there's a many to many relationship between Places:

public virtual int PlaceID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<User> Followers { get; set; }

and Users:

public virtual int UserID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Place> Places { get; set; }

Users can 'follow' many places, places can have many followers.

I would like to store additional information about the relationship - say a rating. So a user could have a rating for each place they follow, of between 1-10, say.

How can I achieve that - including CRUD operations (Code First, Entity Framework 5).

Thanks.

4

2 に答える 2

1

多対多を、以下のように定義できる rating プロパティを持つ中間クラスへの 2 つの 1 対多の関係に置き換える必要があります (ナビゲーション属性を追加する必要がある場合があります)。

中級クラスを作る

public class UserPlaceRelationship {
  public virtual int PlaceID { get; set; }
  public virtual int UserID { get; set; }
  public virtual Place Place { get; set; }
  public virtual User User { get; set; }
  public virtual int Rating { get; set; }
}

プレイス クラスを更新します。

public virtual int PlaceID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<UserPlaceReationship> UserRelationships { get; set; }

そしてあなたのユーザークラス:

public virtual int UserID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<UserPlaceReationship> PlaceRelationships { get; set; }

中間クラスをナビゲートする必要があるため、クエリは少し複雑になります。

于 2013-06-15T08:05:25.453 に答える
0

ジャンクション テーブルを作成し、追加情報を追加する必要があります。

これまで、関連するビューを持つ CRUD マスター/ディテール コントローラーを作成する方法はありません。

あなたは、あなたのこの質問に対して私が提供した答えに従うべきです...

于 2013-07-30T10:04:28.850 に答える