如何从代码中首先访问映射表c#

时间:2016-03-18 07:50:49

标签: c# entity-framework migration many-to-many code-first

 private void ManyToMany_ProductLines_L4(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ProductLine>()
                .HasMany(l => l.L4)
                .WithMany(p => p.ProductLine)
                .Map(
                  m =>
                  {
                      m.MapLeftKey("ProductLinesID");
                      m.MapRightKey("L4ID");
                      m.ToTable("ProductLinesL4");
                  });
        }

  public class ProductLine
    {
        public int ID { get; set; }
        public virtual List<L4> L4 { get; set; }
    }

public class L4
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public virtual List<ProductLine> ProductLine { get; set; }
    }

我有很多关系,我想访问这个表ProductLinesL4,但对于这个表我没有项目中的模型。我有办法访问这个表,还是我需要创建一个模型?

1 个答案:

答案 0 :(得分:1)

是的,如果您想在上下文中直接访问该表,则需要为其创建模型。

如果你需要做某事&#34;&#34;在该表上,您可以选择手动管理多对多关系;如果你需要这样做(例如你需要创建多对多),你可能需要在该表上创建一个PK。

希望有所帮助:)