one-to-many relationship, many part sometime saved withoud foreign key to one

Thread is closed for posting
2 posts, 0 answers
  1. Jeroen
    Jeroen avatar
    15 posts
    Member since:
    Jun 2015

    Posted 01 Dec 2017 Link to this post

    Hello,

     

    I have the following Fluent model:

    var customerOrderLineMapping = new MappingConfiguration<CustomerOrderLine>();
                customerOrderLineMapping.MapType(customerOrderLine => new
                {
                    Amount = customerOrderLine.Amount,
                    Price = customerOrderLine.Price,
                    CustomerOrderID = customerOrderLine.CustomerOrderID,
                    ArticleID = customerOrderLine.ArticleID,
                    PercentDiscount = customerOrderLine.PercentDiscount,
                    TotalPriceExcl = customerOrderLine.TotalPriceExcl,
                    TotalPriceIncl = customerOrderLine.TotalPriceIncl,
                    VAT = customerOrderLine.VAT,
                    Extradited = customerOrderLine.Extradited,
                    SendDate = customerOrderLine.SendDate,
                    Invoiced = customerOrderLine.Invoiced,
                    PickDate = customerOrderLine.PickDate,
                    AdjustedSellingPrice = customerOrderLine.AdjustedSellingPrice,
                    ExternalId = customerOrderLine.ExternalId,         
                    Instruction = customerOrderLine.Instruction,
                    IsDisplay = customerOrderLine.IsDisplay,
                    DisplayProcessed = customerOrderLine.DisplayProcessed,
                    CorrectionProductLineID = customerOrderLine.CorrectionProductLineID,
                    FromArticleSplitting = customerOrderLine.FromArticleSplitting,
                    DateCreated = customerOrderLine.DateCreated,
                    CreatedBy = customerOrderLine.CreatedBy,
                    DateLastChanged = customerOrderLine.DateLastChanged,
                    LastChangedBy = customerOrderLine.LastChangedBy,
                }).ToTable("CustomerOrderLine");
                customerOrderLineMapping.HasProperty(l => l.ID).IsIdentity(KeyGenerator.Autoinc);            
                customerOrderLineMapping.HasAssociation(o => o.CustomerOrder).HasFieldName("customerOrder")
                    .WithOpposite(l => l.CustomerOrderLines).ToColumn("CustomerOrderID")
                    .HasConstraint((l, o) => l.CustomerOrderID == o.ID)
                    .IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
                

                var customerOrderMapping = new MappingConfiguration<CustomerOrder>();
                customerOrderMapping.MapType(customerOrder => new
                {
                    OrderDate = customerOrder.OrderDate,
                    CustomerID = customerOrder.CustomerID,
                    AgentID = customerOrder.AgentID,
                    KnownClient = customerOrder.KnownClient,
                    CreditOrder = customerOrder.CreditOrder,
                    PaymentTermID = customerOrder.PaymentTermID,
                    ExternalOrderReference = customerOrder.ExternalOrderReference,
                    ExternalId = customerOrder.ExternalId,
                    PercentDiscount = customerOrder.PercentDiscount,
                    Discount = customerOrder.Discount,
                    Approved = customerOrder.Approved,
                    ApprovedBy = customerOrder.ApprovedBy,
                    ApprovedDate = customerOrder.ApprovedDate,
                    Printed = customerOrder.Printed,
                    PrintedDate = customerOrder.PrintedDate,
                    PickedDate = customerOrder.PickedDate,
                    EmployeeID = customerOrder.EmployeeID,
                    ExpectedSendDate = customerOrder.ExpectedSendDate,
                    OrderStateID = customerOrder.OrderStateID,
                    DateCreated = customerOrder.DateCreated,
                    CreatedBy = customerOrder.CreatedBy,
                    DateLastChanged = customerOrder.DateLastChanged,
                    LastChangedBy = customerOrder.LastChangedBy,
                }).ToTable("CustomerOrder");
                customerOrderMapping.HasProperty(o => o.ID).IsIdentity(KeyGenerator.Autoinc);                        
                customerOrderMapping.HasAssociation(l => l.CustomerOrderLines).HasFieldName("customerOrderLine")
                    .WithOpposite(o => o.CustomerOrder).ToColumn("ID")
                    .HasConstraint((l, o) => l.ID == o.CustomerOrderID)
                    .IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);

     

    On random occasion it saves orderlines without reference to the order.

    I only call save for the Order.

     

    I use Open Data Access 2015.1.225.1 with SQL Server 2012 SP3.

     

    Is this known, can I prevent it.

     

    Or should I seek the cause elsewher in my code?

     

    Jeroen
  2. Jeroen
    Jeroen avatar
    15 posts
    Member since:
    Jun 2015

    Posted 04 Dec 2017 Link to this post

    After some intensive testing I've found the part in my code where it goes wrong.

     

    At this point I delete the CustomerOrderLines, and save the CustomerOrder Object.

    Apparently this doe not delete the lines, just removes the foreign key.

     

    Jeroen

Back to Top