Rails 在 ASP.NET MVC3 中具有多通等效性
在 .NET 实体框架中,拥有带有额外属性(id 除外)的(自定义)联接表和/或通过单独的模型将此联接表与其他联接表关联的最佳方法是什么?在 Ruby on Rails 中,我们可以有一个连接表模型,例如:
Item.rb (model)
:has_many => :buyers, :through=>:invoice
...
Buyers.rb (model)
:has_many => :items, :through=>:invoice
...
Invoice.rb (model)
:belongs_to :item
:belongs_to :buyer
....
然后我们可以使用: Item.first.buyers
、Buyers.first.items
和 Buyer.create(:items=>Item.create(:name=>'random'))
等,就像我们使用没有模型的自动连接表(使用 has_and_belongs_to_many)一样。
在 Visual Studio 2010 的“添加关联”对话框中,如果我们选择多重性为 *(Many),则没有选择连接表(带模型)的选项。有没有办法手动完成?
In .NET Entity Framework, what is the best way to have a (custom) join table with extra attributes (other than ids) and/or associate this join table with others via separate model? In Ruby on Rails we can have a model for the join table, like:
Item.rb (model)
:has_many => :buyers, :through=>:invoice
...
Buyers.rb (model)
:has_many => :items, :through=>:invoice
...
Invoice.rb (model)
:belongs_to :item
:belongs_to :buyer
....
Then we can use: Item.first.buyers
, Buyers.first.items
and Buyer.create(:items=>Item.create(:name=>'random'))
etc. just like when we use automated join table without model (using has_and_belongs_to_many).
In Visual Studio 2010's "Add Association" dialog, if we select multiplicity as *(Many) there is no option to select a join table (with model). Is there a way to do it manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以获得非常接近的东西。我不太确定如何在设计器中进行设置,因为我只使用代码优先。
这是一个例子:
学生 -> StudentFloor <- Floor
链接表:
最后,多对多的另一边:
Yes, you can get something pretty close. I'm not quite sure how to set this up in the designer since I only work with codefirst.
Here's an example:
Student -> StudentFloor <- Floor
The linking table:
Finally, the other side of the many-to-many:
更新 Leniency 的答案:
我们还可以使用模型优先方法创建两个一对多关系。无论哪种方式,我们都无法像纯 M2M 关系中那样进行模型绑定(没有负载或纯连接表 - PJT)。
此外,在(脚手架)控制器中,我们可以根据要求使用视图模型进行 CRUD 操作。假设我们有一个具有以下定义的 FloorViewModel:
控制器中的创建操作将是:
编辑操作将类似于:
在视图中,我们可以发送 FloorModelView 的对象,如:
最后, _CreateOrEdit 部分如下所示:
UPDATE to Leniency's answer:
We can also create two one-to-many relationships with Model first approach. Either way we cannot have model binding as happen in pure M2M relationship (without payload or pure join tables - PJT).
Also, in (scaffold) controller, we can use a view model for the CRUD operations as per the requirement. Supposedly, we have a FloorViewModel with the following definition:
The create action in controller would be:
and the Edit action would be something like:
In View, we can send the FloorModelView's object like:
and finally, _CreateOrEdit partial looks like: