Linq 2 SQL,将具有关联子对象列表的对象插入数据库,Silverlight RIA [英] Linq 2 SQL , insert Objects with associated child objects List into database , Silverlight RIA

查看:58
本文介绍了Linq 2 SQL,将具有关联子对象列表的对象插入数据库,Silverlight RIA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个小的Silverlight应用程序,它将允许用户创建订单.我创建了Linq 2 SQL dbml,并将数据库表"Orders"和"OrderLines"拖到那里,它们之间存在关联. Order.ID〜OrderLine.OrderID,因此我为表创建了DomainService,在其中启用了客户端访问,它为我生成了用于Orders和OrderLines的Insert,Update,Delete,Get方法,现在我从在我的Silverlight应用程序中,用户控件如下所示:

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});
    }
}

和域服务方法:

 public void InsertOrder(Order order)
    {
        Context.Orders.InsertOnSubmit(order);
        Context.SubmitChanges();
    }

我已经在这里设置了断点,线程来到了这里,一切都OK.但是之后数据库中没有订单,也没有订单行.和输出写道:"System.Data.Linq.dll中发生类型'System.Data.SqlClient.SqlException'的第一次机会异常"

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.

推荐答案

您是从头开始创建订单行吗?如果是这样,则需要确保它们是新的,并且要为要添加的每个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...

这篇关于Linq 2 SQL,将具有关联子对象列表的对象插入数据库,Silverlight RIA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆