Linq 2 SQL , insert Objects with associated child objects List into database , Silverlight RIA

梦想与她 提交于 2019-12-23 18:37:12

问题


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.


回答1:


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...



来源:https://stackoverflow.com/questions/1636618/linq-2-sql-insert-objects-with-associated-child-objects-list-into-database-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!