Linq 2 SQL, insert objects with related child List objects into a database, Silverlight RIA

I am making a small Silverlight application that will allow users to create orders. I created Linq 2 SQL dbml and dragged my database tables, "Orders" and "OrderLines" there, there is a relationship between them. Order.ID ~ OrderLine.OrderID, so I created a DomainService for my tables, where I turned on client access, it created methods for me, Insert, Update, Delete, Get, for Orders and OrderLines, now I create a New Order from my silverlight application , user control is as follows:

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

, , . . : " System.Data.Linq.dll " System.Data.SqlClient.SqlException "

? , , .

+3
1

OrderLines ? , , , Context.OrderLines.InsertOnSubmit(orderLine) OrderLine . , ...

+2

Source: https://habr.com/ru/post/1721285/


All Articles