1
我正在使用LinqToSql作爲ORM。我有下面的代碼形式:指定的演員表在預覽中無效
public partial class frmBarcodeList : Form
{
private ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["myconn"];
private DatabaseContext _context;
public frmBarcodeList()
{
_context = new DatabaseContext(connectionString.ConnectionString);
InitializeComponent();
}
}
在按鈕處理程序點擊我打電話GetInfoFromDataBase
。 這是問題的方法:
private List<TicketInfo> GetInfoFromDataBase()
{
try
{
var oids = GetSelectedEvents();
List<Order> orders = new List<Order>();
List<TicketInfo> data = new List<TicketInfo>();
foreach (var oid in oids)
{
orders.AddRange(_context.Orders.Where(o => o.ScheduleId == oid && o.IsPayed.Value).ToList());
}
foreach (var order in orders)
{
foreach (var detail in order.OrderDetails) ***line 158**
{
var checkSum = CalculateChecksum(detail.BarCode);
TicketInfo info = new TicketInfo();
info.Firstname = order.SiteUser.FirstName;
info.LastName = order.SiteUser.LastName;
info.Email = order.SiteUser.Email;
info.Phone = order.SiteUser.PhoneNumber;
info.Barcode = string.Format("{0}{1}", detail.BarCode, checkSum);
info.FileName = RemoveInvalidFilePathCharacters(string.Format("{0}_{1}", order.EventSchedule.BaseEvent.Name,
order.EventSchedule.RecurrenceStart), "");
data.Add(info);
}
}
return data;
}
catch (Exception e)
{
MessageBox.Show(e.Message + "\n" + e.StackTrace);
}
return null;
}
我得到錯誤:
---------------------------
---------------------------
Specified cast is not valid.
at System.Data.SqlClient.SqlBuffer.get_Int32()
at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
at Read_OrderDetail(ObjectMaterializer`1)
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Data.Linq.EntitySet`1.Load()
at System.Data.Linq.EntitySet`1.GetEnumerator()
at DT.KazBilet2.BarcodeChecker.frmBarcodeList.GetInfoFromDataBase() in C:\Users\Макс\Documents\Visual Studio 2010\Projects\DT.KazBilet2\branches\NewDesk\DT.KazBilet2.BarcodeChecker\frmBarcodeList.cs:line 158
---------------------------
ОК
---------------------------
爲什麼我得到這個錯誤?
謝謝。
看起來像模型錯誤(即數據庫中與模型中的類型不匹配的列類型)。很難更具體。 – 2012-08-08 06:52:57
你可以發佈'OrderDetail'定義和相應的DBML文件片段嗎? – Dennis 2012-08-08 07:13:43
@JoachimIsaksson:是的,你說得對。謝謝 – user1260827 2012-08-08 07:57:19