在 Win7 应用程序中,我尝试使用 EF 中的 EntityClient 访问 SqlServerCe 3.5 (IPManager_DBEntities),更新名为“Channel”的 ADO.NET 数据库表中的多个字段,EntitySetMapping 名称为“Channels”。
使用 VS 2010 IDE,代码编译良好,Intellisense 没有任何抱怨。通道数据表的格式在底部引用,因为行中的各个字段(由通道“编号”选择)需要使用从代码传递给它的信息进行更新,为简单起见,未显示代码。在过去的几天里,我在谷歌上搜索的任何东西都没有解决我的类型转换困境。使用 LINQ,我得到了这个运行时异常:
“无法将‘System.Data.Objects.ObjectQuery`1 [Manager.Data.Channel]’类型的对象转换为‘Manager.Data.Channel’类型”。
// Update channel status with information parsed from the data packet.
using (IPManager_DBEntities context = new IPManager_DBEntities())
{
Channel thisChannelRow = (Channel)(from CE
in context.Channels
where CE. Number == int.Parse(IDLine[2])
select CE);
// Throwing exception after setting up this query:
// "Unable to cast object of type 'System.Data.Objects.ObjectQuery`1
// [Manager.Data.Channel]' to type 'Manager.Data.Channel'"
// During debug sessions, "thisChannelRow" is null as a result.
MessageBox.Show("thisChannelRow. Channel = " + thisChannelRow.Number );
ThisChannel.StatusID = int.Parse(IDLine[5]);
ThisChannel.Cycle = int.Parse(IDLine[4]);
ThisChannel.Modified = DateTime.Now;
context.SaveChanges();
}
我希望有人有一个解决方案来帮助我度过这个困境。