嗨,我正在尝试学习 LINQ,在 LINQ to SQL 中,我遇到了以下异常:这是来自 Manning 出版物的 Linq In Action 的示例代码。怎么了?
DataContext db = new DataContext("E:\\Mahesh\\TempFolder\\DB\\NORTHWND.MDF");
var contacts =
from contact in db.GetTable<Contact>()
where contact.City == "Paris"
select contact;
foreach (Contact aContact in contacts)
Console.WriteLine("Bonjour " + aContact.Name);
Console.Read();
}
}
[Table(Name = "Customers")]
class Contact
{
[Column(IsPrimaryKey = true)]
public int CustomerID { get; set; }
[Column(Name = "ContactName")]
public string Name { get; set; }
[Column]
public string City { get; set; }
}
异常详情:
System.InvalidCastException was unhandled
HResult=-2147467262
Message=Specified cast is not valid.
Source=System.Data
StackTrace:
at System.Data.SqlClient.SqlBuffer.get_Int32()
at Read_Contact(ObjectMaterializer`1 )
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at LinqDemo.Program.Main(String[] args) in c:\Users\MAHESH\Desktop\TechNode\C#\MyTechDos\LinqDemo\LinqDemo\Program.cs:line 51
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: