简体   繁体   中英

Invalid Column Name using Entity Framework

I'm using Entity Framework to generate my model and whilst the generation of the model itself is successful I'm getting the following error when querying the database.

Invalid column name 'EmployeeDealer_EmployeeDealerID'

The code that throws the exception is:

var employee = db.Employees.Where(emp => emp.EmployeeID == employeeId).FirstOrDefault();

It all looks fairly straightfoward so I'm assuming EF hasn't been able to generate the proper keys or ORM classes?

Here's the DB Schema for the related tables.

数据库架构

Any assistance on how to resolve this would be greatly appreciated.

I ended up resolving this by changing the modified classes. Within the constructor in the Employee class I removed the following statement:

this.Employees = new List<Employee>();

and also removed the following property

public virtual ICollection<Employee> Employees { get; set; }

Then again in the EmployeeDealer class I removed from the constructor:

this.Employees = new List<Employee>();

and also the property

public virtual ICollection<Employee> Employees { get; set; }

So I guess the issue lies in auto-generating an entity for a many-to-many table you need to ensure that the references (I'm assuming the navigation path) between the two other tables are produced correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM