订单中有很多人

时间:2015-04-28 12:56:38

标签: entity-framework entity-framework-6

我正在使用它:

modelBuilder.Entity<X>()
.HasMany(u => u.Ys)
.WithMany()
.Map(m =>
{
    m.ToTable("XToYs");
    m.MapLeftKey("XId");
    m.MapRightKey("YId");
});

模拟X和Y之间的多对多关系。有没有办法像这样添加订单:

XId Yid Order
1   2   1
1   3   2

1 个答案:

答案 0 :(得分:0)

基本上你想要做的是:

public class XY
{
    public int XId {get; set;}
    public virtual X X{get; set;}
    public int YId {get; set;}
    public virtual Y Y {get; set;}
    public int Order {get; set;}
}

并在你的modelBuilder中:

builder.Entity<X>()
    .HasMany(x => x.Y)
    .WithRequired()
    .HasForeignKey(k => k.YId);

builder.Entity<Y>()
    .HasMany(y => y.X)
    .WithRequired()
    .HasForeignKey(k => k.XId);