我正在将我的代码从 Visual Studio 2010 迁移到 2012 并确保所有代码都按预期运行。在此转换期间,我的 LINQ-to-SQL 示例程序在尝试将数据插入我的一个表时失败。以下是用户单击保存按钮时的代码:
public class DebugTextWriter : TextWriter
{
public override void Write(char[] buffer, int index, int count)
{
Debug.Write(new String(buffer, index, count));
}
public override void Write(string value)
{
Debug.Write(value);
}
public override Encoding Encoding
{
get
{
return Encoding.Default;
}
}
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
try
{
//First we locate the 'orderViewSource' object that the user filled in
CollectionViewSource orderViewSource =
FindResource("orderViewSource") as CollectionViewSource;
//Next we save the values entered in into a generic list (only 1 entry
here)
List<Order> ordList = orderViewSource.Source as List<Order>;
//Again, since there is only one entry, we take it and fill in a new row in
//our 'Order' table
Order ord = ordList.FirstOrDefault();
//In order for the insert to work, you need to create an instance of the
//LINQ to SQL class for that table and then call the insert method on the
//'ord' container and then finally submit it.
var ctx = new MyShopDataContext();
ctx.Orders.InsertOnSubmit(ord);
ctx.SubmitChanges();
MessageBox.Show("Order Saved!");
}
catch (Exception)
{
}
}
我的代码构建没有错误,也没有抛出异常。关于为什么这段代码不起作用的任何想法?