处理MVC项目中的多对多插入/编辑 [英] Handling many-to-many inserts/edits in a MVC project

查看:82
本文介绍了处理MVC项目中的多对多插入/编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在撰写一个小型MVC游戏评论网站,并且在插入新评论或编辑现有评论时遇到了麻烦。 游戏与平台有多对多的关系。 每个平台上都有很多游戏(PS3,XBox
360等),有些游戏可以在各种平台上使用(比如侠盗猎车手系列)。


<我有一个GamesPlatforms表,但它在模型中不可见,因为它只包含Games键和Platforms键。


我的基本问题是不知道如何处理平台数据。 &NBSP;几个示例场景(现在我的视图中有一个代表平台的复选框列表):


我正在为游戏撰写新评论。 我点击几个复选框 - 比如PS3,XBox 360和PC。 我该怎么处理这些信息? 我如何记录它并将其保存到数据库?


同样地,让我说我搞砸了,我审查的游戏实际上是针对Wii而不是DS。 如何删除游戏和平台之间的原始链接并创建和存储新的链接。


编辑:我想我应该提供更多细节。


 


游戏:

   int ID(主键,auto-inc)

   string GameTitle

   string ReviewTitle

  字符串分数

   int ReviewContentID(Content表中的外键,包含新闻,文章和评论的Text和DateAdded属性)

   int GenreID(类型表中的外键)


GamePlatform(模型本身不可见):

   int GameID(游戏桌上的外键)

   int PlatformID(Platform表中的外键)


平台:

   int ID(主键,auto-inc)

  字符串名称


我对如何编辑现有平台数据感到困惑。 我的视图中有一行复选框,其中每个框的值都是现有平台ID的值。 我遇到的最大问题是可能从关系中移除现有平台
。 再次,就像我上面的示例一样,将游戏平台从DS更改为Wii。 或者,说我在PS3,XBox 360和PC类别中意外地拥有一个游戏,当它实际上是XBox专属时。 那将需要两个GamePlatform
删除,我不知道该怎么做。

解决方案

你可以看到以下帖子介绍了如何处理多方与多方的情景。

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/90097c2a-3dc5-4e2a-af19-fe76cf3be6f4


I'm currently writing a small MVC game review site, and have hit a snag when it comes to inserting new reviews, or editing existing ones.  Games have a many-to-many relationship with platforms.  There are many games on each platform (PS3, XBox 360, etc.) and some games are available on a variety of platforms (the Grand Theft Auto series, for instance).

I have a GamesPlatforms table, but it's not visible in the model since it just contains the Games key and Platforms key.

My basic problem is not knowing how to handle the platform data.  A couple of example scenarios (right now my view has a list of checkboxes representing the platforms):

I'm writing a new review for a game.  I click on a few of the checkboxes - let's say PS3, XBox 360, and PC.  What would I do with that information?  How would I record it and save it to the db?

Similarly, let's say I messed up and a game I reviewed is actually for the Wii and not the DS.  How would I remove the original link between game and platform(s) and create and store the new one(s).

EDIT: I figured I should give more detail.

 

Game:
   int ID (primary key, auto-inc)
   string GameTitle
   string ReviewTitle
   string Score
   int ReviewContentID (foreign key from the Content table, which holds the Text and DateAdded properties for News, Articles, and Reviews)
   int GenreID (foreign key from the Genre table)

GamePlatform (not visible in the model itself):
   int GameID (foreign key from the Game table)
   int PlatformID (foreign key from the Platform table)

Platforms:
   int ID (primary key, auto-inc)
   string Name

I'm mostly confused with how I would edit existing platform data.  I have a row of checkboxes in my view where each box's value is that of an existing Platform ID.  The biggest problem I'm having is with potentially removing existing platforms from the relationship.  Again, like in my example above, changing a game's platform from DS to Wii.  Or, say I accidentally had a game in the PS3, XBox 360, and PC categories when it's actually a XBox exclusive.  That would require two GamePlatform removals, and I'm not sure how to do that.

解决方案

You can see the following post about how to deal the scenario with many to many relationship.
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/90097c2a-3dc5-4e2a-af19-fe76cf3be6f4


这篇关于处理MVC项目中的多对多插入/编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆