ASP。NET MVC 4, EF5,将更多的数据存储到许多连接表中。

时间:2022-06-02 00:17:47

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

我有一个代码第一MVC 4的Web应用程序-有很多很多地方之间的关系:

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.

我想存储更多关于这种关系的信息——比如评级。因此,用户可以对他们跟随的每个地方都有一个评级,在1-10之间。

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

如何实现这一点——包括CRUD操作(代码优先,实体框架5)。

Thanks.

谢谢。

2 个解决方案

#1


1  

You will need to replace the many-to-many with two one-to-many relationships to an intermediate class that has the rating property which could be defined as below (you may need to add navigation attributes):

您将需要用两个一对多关系替换多对多关系,以替换为具有评级属性的中间类(您可能需要添加导航属性):

Create an intermediate class

创建一个中间类

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; }
}

Update your places class:

更新您的地方类:

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

and your users class:

和你的用户类:

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

Querying will be a little trickier as you will have to now navigate through the intermediate class.

查询将更加复杂,因为您现在必须在中间类中导航。

#2


0  

You must create a junction table and add extra info to it.

您必须创建一个连接表并向其添加额外的信息。

Till now, there is no way to create a CRUD master-detail controller with related views.

到目前为止,还没有办法创建具有相关视图的CRUD主-细节控制器。

You should follow the answer I provided to this question of yours...

你应该按照我为你的问题提供的答案去做……

#1


1  

You will need to replace the many-to-many with two one-to-many relationships to an intermediate class that has the rating property which could be defined as below (you may need to add navigation attributes):

您将需要用两个一对多关系替换多对多关系,以替换为具有评级属性的中间类(您可能需要添加导航属性):

Create an intermediate class

创建一个中间类

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; }
}

Update your places class:

更新您的地方类:

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

and your users class:

和你的用户类:

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

Querying will be a little trickier as you will have to now navigate through the intermediate class.

查询将更加复杂,因为您现在必须在中间类中导航。

#2


0  

You must create a junction table and add extra info to it.

您必须创建一个连接表并向其添加额外的信息。

Till now, there is no way to create a CRUD master-detail controller with related views.

到目前为止,还没有办法创建具有相关视图的CRUD主-细节控制器。

You should follow the answer I provided to this question of yours...

你应该按照我为你的问题提供的答案去做……