EF MVC4 C#:与“关系属性”DB的多对多关系

时间:2013-08-10 19:05:15

标签: c# entity-framework asp.net-mvc-4 foreign-keys many-to-many

我已经花了最近几天试图解决我的问题,遗憾的是没有任何结果。我已经在这里看过无数的关于这个主题的帖子了,但是我一直得到同样的错误。 “'字段列表'中的未知列'Extent1.foo_id'”......我做错了什么?我的映射在某种程度上是错误的,但我没有看到如何......

编辑:首先是数据库!

我还有另一个类“Doo”,它与“Foo”有很多很多关系,但是那个工作正常。

提前致谢!

  public class Foo
        {

        public Foo()
            {
            this.FooBoo = new Collection<FooBoo>();
            }        

        public String FooId { get; set; }        
        public virtual ICollection<FooBoo> FooBoo { get; set; }

        }


         public class Boo
            {

            public Boo()
                {
                this.FooBoo = new Collection<FooBoo>();    
                }

                    public String BooId { get; set; }    
                    public virtual ICollection<FooBoo> FooBoo { get; set; }  
                }

         public class FooBoo
            {        
                public String Fooid { get; set; }

                public virtual Foo Foo { get; set; }

                public String Booid { get; set; }        

                public virtual Boo Boo { get; set; } 

                public Boolean RandomProperty { get; set; }       

            }

         public class BooMapper : EntityTypeConfiguration<Boo>
            {

                public BooMapper()
                {

                    this.HasKey(t => t.BooId);


                    this.Property(t => t.BooId).HasColumnName("booid");

            this.ToTable("boo", "fooboodb");

                    this.HasMany(t => t.FooBoo)
                        .WithRequired()
                        .HasForeignKey(t => t.Booid);
                }
            }


         public class FooMapper : EntityTypeConfiguration<Foo>
            {

                public FooMapper()
                {

                    this.HasKey(t => t.FooId);


                    this.Property(t => t.FooId).HasColumnName("fooid");
                        .
            this.ToTable("foo", "fooboodb");

                    this.HasMany(t => t.FooBoo)
                        .WithRequired()
                        .HasForeignKey(t => t.Booid);
                }
            }

         public class FooBooMapper : EntityTypeConfiguration<FooBoo>
            {
            public FooBooMapper()
                {

                this.HasKey(t => new {t.Fooid, t.Booid});

                this.Property(t => t.Fooid);

                this.Property(t => t.Booid);

                this.Property(t => t.RandomProperty);

                this.ToTable("fooboo", "fooboodb");
                this.Property(t => t.Fooid).HasColumnName("Fooid"); 
                this.Property(t => t.Booid).HasColumnName("Booid");
                this.Property(t => t.RandomProperty).HasColumnName("randomproperty");

                }

            }

1 个答案:

答案 0 :(得分:1)

您必须为两个WithRequired调用提供lambda表达式才能指定反向导航属性。否则EF将假定它们属于另一个附加关系,这种关系导致带有下划线的外键:

public class BooMapper : EntityTypeConfiguration<Boo>
{
    public BooMapper()
    {
        //...

        this.HasMany(t => t.FooBoo)
            .WithRequired(fb => fb.Boo)
            .HasForeignKey(t => t.Booid);
    }
}

public class FooMapper : EntityTypeConfiguration<Foo>
{
    public FooMapper()
    {
        //...

        this.HasMany(t => t.FooBoo)
            .WithRequired(fb => fb.Foo)
            .HasForeignKey(t => t.Booid);
    }
}