0
[Table("Pages")] public class Page { [Key, Column("Page_Id")] public int ID { get; set; } public string Name {get;set;} public virtual ICollection<Category> Categories { get; set; } public Page() { this.Categories = new HashSet<Category>(); } } [Table("Categories")] public class Category { [Key] public int Category_ID { get; set; } public string Name {get;set;} public virtual ICollection<Page> Pages { get; set; } public Category() { this.Pages = new HashSet<Page>(); } } [Table("CategoryPages")] public class CategoryPage { [Key, Column(Order = 0)] public int category_ID { get; set; } [Key, Column(Order = 1)] public int page_ID { get; set; } public virtual Page Page { get; set; } public virtual Category Category { get; set; } } public partial class Context : DbContext { public Context() : base("name=Context") { } public virtual DbSet<Page> Pages { get; set; } public virtual DbSet<Category> Categories { get; set; } public virtual DbSet<CategoryPage> CategoryPages { get; set; } } 

When try to access categories like below code (from method from repository)

 int Id = 1; Page page = context.Pages.Where(x => x.ID == Id).FirstOrDefault(); Category category = page.Categories.FirstOrDefault(); return category; 

Gives me error

{"An error occurred while executing the command definition. See the inner exception for details."} Inner Exception : {"Invalid object name 'dbo.PageCategories'."}

I don't have table name dbo.PageCategories, I renamed dbo.PageCategories to dbo.Categories

Still it's searching for this table.

4
  • Your code does not contain any "PageCategories". This code cannot produce the error you pasted. Commented Sep 1, 2017 at 22:31
  • that's what my concern, I search PageCategories in entire solution gave me 0 result but it produce error like that. Commented Sep 1, 2017 at 22:42
  • Hi, could you rebuild the project that contains [Table("CategoryPages")] ? i believe, either that one, or the category one is renamed. Anyway, you could always peek into the binaries using ILSpy. Commented Sep 1, 2017 at 23:42
  • I could able to build, i get error when i try to access that entity property runtime. Commented Sep 1, 2017 at 23:48

1 Answer 1

0

I don't see anything wrong with the code. I believe you have renamed the table after generating the database, which means the models and the database are out of sync. You have to add a new migration for that (or remove the existing one and add a new one), and then you should be able to update the database.

If you haven't worked with EF Code First Migrations, this MSDN Article should help.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.