EF MVC4 C#:与“;关系属性“;DB优先

本文关键字:属性 DB 优先 关系 MVC4 EF | 更新日期: 2023-09-27 18:27:21

我已经花了几天时间试图解决我的问题,遗憾的是没有任何结果。我已经在这里读过无数关于这个主题的帖子,但我总是犯同样的错误。"'field-list'中的未知列'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");
                }
            }

EF MVC4 C#:与“;关系属性“;DB优先

必须为两个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);
    }
}