在 ASP.NET MVC3 中 Rails 有很多等价物 [英] Rails has-many-through equivalent in ASP.NET MVC3

查看:17
本文介绍了在 ASP.NET MVC3 中 Rails 有很多等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .NET Entity Framework 中,让(自定义)连接表具有额外属性(ID 除外)和/或通过单独的模型将此连接表与其他连接表关联的最佳方法是什么?在 Ruby on Rails 中,我们可以有一个连接表的模型,例如:

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
....

然后我们可以使用:Item.first.buyersBuyers.first.itemsBuyer.create(:items=>Item.create(:name=>'random')) 等等,就像我们使用没有模型的自动连接表(使用 has_and_belongs_to_many)一样.

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).

在 Visual Studio 2010 的添加关联"对话框中,如果我们选择多重性为 *(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?

推荐答案

是的,您可以获得非常接近的东西.我不太确定如何在设计器中进行设置,因为我只使用 codefirst.

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.

这是一个例子:

学生 -> 学生楼层 <- 楼层

Student -> StudentFloor <- Floor

public class Student
{
    public int Id { get; set; }
    // ... properties ...

    // Navigation property to your link table
    public virtual ICollection<StudentFloor> StudentFloors { get; set; }

    // If you wanted to have a property direct to the floors, just add this:
    public IEnumerable<Floor> Floors
    {
        get
        {
            return StudentFloors.Select(ft => ft.Floor);
        }
    }
}

链接表:

public class StudentFloor
{
    #region Composite Keys

    // Be sure to set the column order and key attributes.
    // Convention will link them to the navigation properties
    // below.  The database table will be created with a
    // compound key.

    [Key, Column(Order = 0)]
    public int StudentId { get; set; }

    [Key, Column(Order = 1)]
    public int FloorId { get; set; }

    #endregion

    // Here's the custom data stored in the link table

    [Required, StringLength(30)]
    public string Room { get; set; }

    [Required]
    public DateTime Checkin { get; set; }

    // Navigation properties to the outer tables
    [Required]
    public virtual Student Student { get; set; }

    [Required]
    public virtual Floor Floor { get; set; }

}

最后,多对多的另一面:

Finally, the other side of the many-to-many:

public class Floor
{
    public int Id { get; set; }
    // ... Other properties.

    public virtual ICollection<StudentFloor> StudentFloors { get; set; }
}

这篇关于在 ASP.NET MVC3 中 Rails 有很多等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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