5

I have a composite table called ImporterState, that are tied to a table called Importer and State. The error happens here context.Importers.Include(q => q.States). Why is this happening?

{"Invalid object name 'ImporterStates'."}

    [Table("HeadlineWebsiteImport", Schema = "GrassrootsHoops")]
        public class Importer
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string RssUrl { get; set; }
            public string Type { get; set; }
            public string Keywords { get; set; }
            public bool Active { get; set; }
            public DateTime DateModified { get; set; }
            public DateTime DateCreated { get; set; }

            public int WebsiteId { get; set; }

            public HeadlineWebsite Website { get; set; }

            [InverseProperty("Importers")]
            public ICollection<State> States { get; set; }
        }

[Table("State", Schema = "GrassrootsHoops")]
    public class State
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Abbr { get; set; }

        [InverseProperty("States")]
        public ICollection<Headline> Headlines { get; set; }

        [InverseProperty("States")]
        public ICollection<Importer> Importers { get; set; }
    }
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • EF maybe referring to your join table `ImporterStates` in `dbo` schema. Check [this answer](http://stackoverflow.com/questions/6028375/entity-framework-code-first-many-to-many-setup-for-existing-tables/6028660#6028660) – Eranga Aug 23 '11 at 06:59
  • I am wanting to do all attributes instead of that method. But yes it is doing something with my join table but I am not referencing it anywhere in my code. – Mike Flynn Aug 23 '11 at 15:50
  • attribute based configuration is very limited – Eranga Aug 23 '11 at 16:08
  • Got yea. I will check out the other path. – Mike Flynn Aug 23 '11 at 16:19

1 Answers1

9

The many to many is not possible using attributes only.

try using something like:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Importer>()
            .HasMany(i => i.States)
            .WithMany(s => s.Importers)
            .Map(m =>
                {
                    m.MapLeftKey("ImporterId");
                    m.MapRightKey("StateId");
                    m.ToTable("ImporterState");
                });
    }
Cosmin Onea
  • 2,698
  • 1
  • 24
  • 27
  • Perfect, I landed here because EF6 was auto-handling Many to Many relationships for me on a code base I inherited. So one day it went bang and started complaining about a table that that did not exist. Looking closely at the table, it has got the table name wrong way round, like back to front and front to back kind of thing. The fix was to explicitly specify the relationship as answered above. – IbrarMumtaz Nov 18 '16 at 12:12