Unable to determine the main end of the relationship when saving an order - EF6

I am adding EF6 and trying to recreate a model of my old db. I am also creating a WCF web service. There are too many fields, so I minimized this example to better identify the problem.

I am trying to enter some data into the database for an order. An order includes OrderLines, and each OrderLine can be licensed. In rare cases, OrderLine is one of many, so I have to do it for many. If there is one OrderLine and License for the part, the next OrderLine and License for Maintenance for this part. The part license must refer to the service license.

enter image description here

Problem with ParentLicenceId. In my previous db, there is a relationship between one License and another License supporting PartType. Therefore, if a customer buys Part X, they can take 1 year of service for Part X. Thus, the service license for Part X will display the Part X license as its parent.

using OrderExample; using System; using System.Collections.Generic; namespace OrderExampleCmd { class Program { static void Main() { var omc = new OrderExampleEntities(); var order = new Order { OrderNumber = new Guid().ToString(), OrderLines = new List<OrderLine>() }; // Maitenance var orderLine1 = new OrderLine { OrderLineNumber = 1, Licenses = new List<License>() }; var orderLine1License = new License { LicenseType = "Maintenance" }; orderLine1.Licenses.Add(orderLine1License); // Part var orderLine2 = new OrderLine { OrderLineNumber = 2, Licenses = new List<License>() }; var orderLine2License = new License { LicenseType = "Part", MaintenanceLicense = orderLine1License }; orderLine1License.PartLicenses.Add(orderLine2License); order.OrderLines.Add(orderLine1); order.OrderLines.Add(orderLine2); omc.Orders.Add(order); omc.SaveChanges(); } } } 

In omc.SaveChanges (), an error message appears:

Unable to determine the primary end of the OrderExampleModel.LicenseParentLicense relationship. Multiple objects added can have the same primary key.

I tried:

I looked through several other articles. I hope that there are only some additional settings that I need to apply.

How do I get through this error to save these changes?

+5
source share
1 answer

The error is caused by temporary non-unique primary keys found in attached objects. You need to assign temporary primary keys and foreign keys for order lines.

When a new object is created, the Entity Framework defines the temporary and sets the IsTemporary property to true. When you call SaveChanges, the Entity Framework assigns a constant key and the IsTemporary property sets to false. - MSDN

By default, orderLine1 and orderLine2 have the same temporary primary keys, which are 0 (the default value for the integer), you need to set them temporary so that 2 order lines have the same temporary identifier (i.e. 0).

 orderLine1.OrderLineId = 1; orderLine1License.OrderLineId = 1; orderLine2.OrderLineId = 2; orderLine2License.OrderLineId = 2; 
+10
source

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


All Articles