locked
Invalid object error in EF RRS feed

  • Question

  • User1295747327 posted

    I want to get product and all its images. but i am getting invalid object error

     [HttpGet("prodimages")]
            public IActionResult ProductWithImages(int PID=2)
            {
                var productsWithImages = _context.Product.Where(p => p.Id == PID)
                    .Include(p => p.Images).ToList();
               
                return Ok(new { status = "200", product = productsWithImages, gender = "M" });
            }
     public class Product
        {
            public int Id { get; set; }
    
            public string Name { get; set; }
            public ICollection<ProductImage> Images { get; set; }
         }
    
     public class ProductImage
        {
            public int Id { get; set; }
    
            public string ImageName { get; set; }
            public string ImageSize { get; set; }
            public int ProductId { get; set; }
            public Product Product { get; set; }
        }

    I am getting error "Invalide object ProductImage"

    Do i need to create some relationship?

    Tuesday, January 28, 2020 7:33 AM

Answers

  • User1295747327 posted

    After wasting too much time i found the reason. Table name in DB was ProductImages while model class name was ProductImage

    Is there any rules for that?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 28, 2020 8:39 AM
  • User753101303 posted

    Hi,

    This is the default behavior but you can remove this if you want: https://edspencer.me.uk/posts/2012-03-13-entity-framework-plural-and-singular-table-names/

    Try to always provide enough information.  Rather than "Invalid object ProductImage" you likely had something such as "SqlException: Invalid object name "dbo.ProductImage" which better allows to grasp immediately this is a db side table naming isssue rather than wondering first what could make C# or EF to complain about an "invalid object".

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 28, 2020 12:24 PM

All replies

  • User1120430333 posted

    What about the 'virtual' keyword?

    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    
    namespace Entities
    {
       [DataContract] 
        public class DTOStudent
        {
            
            private DTOResponse dtor = new DTOResponse();
    
            [DataMember]
            public Int32 StudentID { get; set; }
    
            [DataMember]
            public string LastName { get; set; }
    
            [DataMember]
            public string FirstName { get; set; }
    
            [DataMember]
            public DateTime? EnrollmentDate { get; set; }
    
            [DataMember]
            public virtual ICollection<DTOEnrollandCourse> EnrollsandCourses { get; set; }
    
            [DataMember]
            public DTOResponse DtoResponse
            {
                get { return dtor; }
                set { dtor = value; }
            } 
        }
    }
    

    Tuesday, January 28, 2020 8:28 AM
  • User1295747327 posted

    After wasting too much time i found the reason. Table name in DB was ProductImages while model class name was ProductImage

    Is there any rules for that?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 28, 2020 8:39 AM
  • User753101303 posted

    Hi,

    This is the default behavior but you can remove this if you want: https://edspencer.me.uk/posts/2012-03-13-entity-framework-plural-and-singular-table-names/

    Try to always provide enough information.  Rather than "Invalid object ProductImage" you likely had something such as "SqlException: Invalid object name "dbo.ProductImage" which better allows to grasp immediately this is a db side table naming isssue rather than wondering first what could make C# or EF to complain about an "invalid object".

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 28, 2020 12:24 PM
  • User1120430333 posted

    After wasting too much time i found the reason. Table name in DB was ProductImages while model class name was ProductImage

    Is there any rules for that?

    Use DB first approach and let EF build the model from the existing DB schema. 

    Tuesday, January 28, 2020 4:20 PM
  • User753101303 posted

    Good point. It seems you are trying to write code by hand for an existing database. You also have the "code first from database" option that could be easier: 

    https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/workflows/existing-database#3-reverse-engineer-model (assuming EF6)

    Tuesday, January 28, 2020 4:30 PM