1
IQueryable<Product> product = objContext.Set<Product>().Include(p => 
p.Categories.Name).Where(p => p.Id == 2);

As per the current view, I'm getting an error. It says add other model with their properties. i.e. to include Category model and corresponding Name property.

@model IEnumerable<>crudOneToMany.Models.Product>

using viewmodel, is it possible to join two tables?

View

Error A specified Include path is not valid. The EntityType 'crudOneToMany.Models.Category' does not declare a navigation property with the name 'Name'.

 public class Product  
    {  
        public int Id { get; set; }  
        public string Name { get; set; }  
        public string Description { get; set; }  
        public int CategoryId { get; set; }  

        public virtual Category Categories { get; set; }  
    }


public class Category  
    {  
        public int Id { get; set; }  
        public string  Name { get; set; }  

        public ICollection<Product> Products { get; set; }  
    }  


public class ProductDBContext : DbContext
    {
        public ProductDBContext()
            : base("ProductDBContext")
        {

        }

        public DbSet<Product> Products { get; set; }
        public DbSet<Category> Categories { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>().HasRequired(o => o.Categories).WithMany(o => o.Products).HasForeignKey(o => o.CategoryId);
            base.OnModelCreating(modelBuilder);
        }
    }
5
  • Yes,ViewModel's whole purpose is for that. What is your issue ? Can you share your viewmodel and the way you have filled it ?
    – Sampath
    Apr 29, 2016 at 9:34
  • Check out now. thank you for your efforts Apr 29, 2016 at 9:47
  • Don't you have ViewModel right now for the above scenario ? What kind of knowledge do you have about viewmodel ?
    – Sampath
    Apr 29, 2016 at 9:52
  • I dont have knowledge, saw few videos. I have to learn it from scratch Apr 29, 2016 at 10:00
  • using viewmodel will it get solved? merging of two tables on primary key will be done? Apr 29, 2016 at 10:00

2 Answers 2

0

Your problem is here:

.Include(p => p.Categories.Name)

Instead you should write .Include(p => p.Categories) This means that in output there will be loaded Categories navigation collection to product. Name is simple string property (is not navigation property so it should not be included)

2
  • Then how can he shows the category list on his view ? : i.stack.imgur.com/KxZZN.png
    – Sampath
    Apr 29, 2016 at 10:18
  • and what is the problem to create foreach(var item in Model.Categories)? If there would be code of view, i would change it, but i don't want to wrote whole view. Apr 29, 2016 at 10:46
0

Here is the proposed ViewModel for you.

ProductViewModel.cs

public class ProductViewModel
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "required")]
        public string ProductName { get; set; }
               
        public Category  Category  { get; set; }

        public ICollection<Category> Categories { get; set; }
   }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.