简体   繁体   English

LINQ to SQL:无效的列名称'DepartureGate',即使该列存在

[英]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. 我正在运行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. 数据库更改后,您应该更新类(将表拖放到linq to sql文件中),以便可以使用对象名称访问该列。 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. 如果您的栏位是varchar类型,请尝试varchar(MAX)。

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. 遇到此错误时,我发现我的一个Model对象上有一些虚假的[Key]属性,这些属性实际上并不是该对象表的唯一键定义的一部分。 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. 在此之前,删除表,然后再次将表拖到DBML文件上并保存DBML。

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

Now you can access: 现在您可以访问:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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