c# - EF MVC4 C# : Many to Many Relationship with "relationship property" DB First

标签 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");

                }

            }

最佳答案

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

关于c# - EF MVC4 C# : Many to Many Relationship with "relationship property" DB First,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18165442/

相关文章:

C# Mvc resx 带有重音字母的转义问题

c# - 域驱动设计布局问题

c# - 如何从具有 EF 模型的程序集中获取 csdl、ssdl 和 msl 规范

sql-server - 使用 Entity Framework Database.Create() 时指定 SQL Server 文件位置

c# - MVC 捆绑 - 包含单个 bundle 的 .min 文件

java - Android - 将 XML 架构发布到 PHP 服务器 API

c# - 如何从 URL 中获取网页标题和图片?

c# - 将数据库上下文提供给对象工厂

javascript - 动态同时返回多个PartialView

c# - 一次触发多次的ajax调用