1

I have 3 tables in my database and these are their EF models:

public class Customer
{
    [Key]
    public string Name { get; set; }
    public string ContactID { get; set; }
    public string VehicleID { get; set; }
    public IList<Contact> Contacts { get; set; }      
    public IList<Vehicle> Vehicles { get; set; }
}
public class Vehicle
{
   [Key]
   public string VehicleID {get; set;}
   public string Make {get; set;}
}

public class Contact
{
   [Key]
   public string ContactID {get; set;}
   public string Name {get; set;}
}

The db context:

protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<Customer>().HasKey(q => new { q.ContactID, q.VehicleID });
         }
        //Defining entities for context class
        public DbSet<Contact> Contacts { get; set; }
        public DbSet<Vehicle> Vehicles { get; set; }
        public DbSet<Customer> Customers { get; set; }

In my controller:

public IEnumerable<Customer> GetCustomers()
        {

            return _context.Customer.Include(c => c.Contacts).Include(v => v.Vehicles);
                
        }

My goal is to return data from the three tables but when I run this I get the error: SqlException: Invalid column name CustomerContactID CustomerVehicleID

Any help appreciated

Thanks!

devlin carnate
  • 8,309
  • 7
  • 48
  • 82
  • Does this answer your question? [Entity Framework - Invalid Column Name '\*\_ID"](https://stackoverflow.com/questions/19959256/entity-framework-invalid-column-name-id) – devlin carnate Sep 04 '20 at 21:32
  • I am using IList not I Collections – Sdeeeeeeeeeeee Sep 04 '20 at 22:37
  • 1
    mine still working. I use IList also. you need to find where are CustomerContactID and CustomerVehicleID in. they are not found in your model. you need to check database too. – Asherguru Sep 05 '20 at 11:03
  • You should look into ViewModels where you flatten things out in the controller, send it to the view in a friendly manner, return it to the controller and update the entity(s). A tool like Automapper makes this easy. Now you can separate the concerns of entities versus what you want for the view. See [here](https://wildermuth.com/2015/07/22/Mapping_Between_Entities_and_View_Models). – Steve Greene Sep 05 '20 at 14:40
  • You can create a dto class with your final view then query all the data within this model. You can use your context object multiple times to get data from different tables. – MonkeyDLuffy Sep 07 '20 at 16:07

0 Answers0