Linq 2 SQL,将具有关联子对象列表的对象插入数据库,Silverlight RIA
我正在做小型 Silverlight 应用程序,它允许用户创建订单。我创建了 Linq 2 SQL dbml 并将我的数据库表“Orders”和“OrderLines”拖到那里,它们之间存在关联。 Order.ID ~ OrderLine.OrderID,所以我为我的表创建了 DomainService,在其中启用了客户端访问,它为我生成了用于订单和 OrderLines 的方法,插入、更新、删除、获取,现在我正在创建新订单我的 silverlight 应用程序,用户控件如下所示:
public partial class NewOrderView : UserControl
{
public ObservableCollection<OrderLine> OrderLines { get; set; }
public NewOrderView()
{
InitializeComponent();
OrderLines = new ObservableCollection<OrderLine>();
dataGrid.ItemsSource = OrderLines;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var order = new Order();
foreach (var orderLine in OrderLines)
{
order.OrderLines.Add(orderLine);
}
order.CompanyId = int.Parse(StaticContainer.CurrentUser.CompanyId.ToString());
order.CreationDate = DateTime.Now;
order.Status = "შეკვეთილი";
order.Id = 1;
var ctx = new EntreeDomainContext();
ctx.Orders.Add(order);
ctx.SubmitChanges();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
StaticContainer.Navigation.Back();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
StaticContainer.Navigation.LogOut();
}
private void AddProduct(object sender, ProductAdditionEventHandlerArgs args)
{
var result = OrderLines.Where(x => x.Item.itmKEY == args.Product.itmKEY).FirstOrDefault();
if(result==null)
OrderLines.Add(new OrderLine(){Item = args.Product});
}
}
域服务方法:
public void InsertOrder(Order order)
{
Context.Orders.InsertOnSubmit(order);
Context.SubmitChanges();
}
我在这里放置了断点,线程来到这里,一切都完成了。但之后数据库中不存在订单和订单行。输出写道:“System.Data.Linq.dll 中发生了类型为‘System.Data.SqlClient.SqlException’的第一次机会异常”,
我该怎么办? 请帮忙,谢谢。
I am doing small Silverlight app which will allow users to Create orders. I have created Linq 2 SQL dbml and dragged there my database tables, "Orders" and "OrderLines" , there is an association between them. Order.ID ~ OrderLine.OrderID, so then i created DomainService for my tables, where i enabled client access, it generated for me the methods, Insert,Update,Delete,Get, for Orders and OrderLines, now i am creating New Order from my silverlight application, the usercontrol looks like this:
public partial class NewOrderView : UserControl
{
public ObservableCollection<OrderLine> OrderLines { get; set; }
public NewOrderView()
{
InitializeComponent();
OrderLines = new ObservableCollection<OrderLine>();
dataGrid.ItemsSource = OrderLines;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var order = new Order();
foreach (var orderLine in OrderLines)
{
order.OrderLines.Add(orderLine);
}
order.CompanyId = int.Parse(StaticContainer.CurrentUser.CompanyId.ToString());
order.CreationDate = DateTime.Now;
order.Status = "შეკვეთილი";
order.Id = 1;
var ctx = new EntreeDomainContext();
ctx.Orders.Add(order);
ctx.SubmitChanges();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
StaticContainer.Navigation.Back();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
StaticContainer.Navigation.LogOut();
}
private void AddProduct(object sender, ProductAdditionEventHandlerArgs args)
{
var result = OrderLines.Where(x => x.Item.itmKEY == args.Product.itmKEY).FirstOrDefault();
if(result==null)
OrderLines.Add(new OrderLine(){Item = args.Product});
}
}
and Domain Service Method:
public void InsertOrder(Order order)
{
Context.Orders.InsertOnSubmit(order);
Context.SubmitChanges();
}
i have put here break point, and the thread comes here , and everything dones ok. but after that in the database no order and no orderline exist. and output writes : "A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.Linq.dll"
what can i do?
please help, and thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否从头开始创建订单行?如果是这样,您需要确保它们是新的,并且您要为要添加的每个 OrderLine 调用 Context.OrderLines.InsertOnSubmit(orderLine) ,否则您会遇到这样的问题。另外,您应该在此处提供所有异常详细信息...
Are you creating OrderLines from scratch? If so, you need to make sure that if they're new, that you're calling Context.OrderLines.InsertOnSubmit(orderLine) for each OrderLine you're adding, or you'll get problems like this. Also, you should provide all exception details here...