Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First and Invalid Object Name Error

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; }
    }
like image 543
Mike Flynn Avatar asked Aug 23 '11 06:08

Mike Flynn


1 Answers

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");
                });
    }
like image 182
Cosmin Onea Avatar answered Nov 14 '22 10:11

Cosmin Onea