简体   繁体   中英

LINQ to SQL: Invalid column name 'DepartureGate', even though the column exists

I absolutely do not get this. The column exists in the table, I've ensured that the application is executing the query against the proper table in the proper database, and it still reports that it's an invalid column name.

I'm running NET 4.0, SQL Server 2008 Express Edition. Does anyone have any similar experience?

Executing queries against any other column name in the same table in the same database works extremely excellently. I added this column today and for some reason my application refuses to acknowledge the existence of this column.

Relevant column definition: 在此处输入图片说明

Relevant code:

(from x in flightDataContext.FlightDatas
where x.FlightDataId == FlightDataID && x.Departure == true
select new
{
  x.ArrivalStationCode,
  x.ArrivalStationName,
  x.DepartureTime,
  x.DepartureGate
}).SingleOrDefault();

I also faced the same problem.You can try the following solutions.

You should update your classes after the database changed(drag and drop your tables to the linq to sql file), so that the column is accessible with object name. if still problem exists then the value you are saving in the coloumn is greater then the size specified for the coloumn. try varchar(MAX) if your coloumn is of varchar type.

When I encountered this error, I discovered that I had some spurious [Key] attributes on one of my Model objects that wasn't actually part of the unique key definition for that object's table. They had been innocuously sitting there until I refactored another of the object's relationships. Once I removed them, the error about the columns being invalid vanished.

Try this. Before this , delete the table and drag drog the table again on the DBML file and save the DBML.

var flight=    flightDataContext.FlightDatas.SingleOrDefault(x=>x.FlightDataId == FlightDataID && x.Departure);

Now you can access:

  flight.ArrivalStationCode,
  flight.ArrivalStationName,
  flight.DepartureTime,
  flight.DepartureGate

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