This is a migrated thread and some comments may be shown as answers.

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

1 Answer 17 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jeroen
Top achievements
Rank 1
Iron
Jeroen asked on 01 Dec 2017, 02:13 PM

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

1 Answer, 1 is accepted

Sort by
0
Jeroen
Top achievements
Rank 1
Iron
answered on 04 Dec 2017, 08:52 AM

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

Tags
Data Access Free Edition
Asked by
Jeroen
Top achievements
Rank 1
Iron
Answers by
Jeroen
Top achievements
Rank 1
Iron
Share this question
or