无法确定保存订单时关系的主要终点-EF6 [英] Unable to determine the principal end of the relationship on saving Order - EF6

查看:46
本文介绍了无法确定保存订单时关系的主要终点-EF6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在添加EF6,并尝试重新创建旧数据库的模型.我也在制作WCF Web服务.字段太多,因此我将示例最小化以更好地查明问题.

I am adding EF6 and trying to recreate the model of my legacy db. I am also making a WCF web service. There are way too many fields, so I've minimized the example to better pinpoint the problem.

我正在尝试将一些数据输入到订单数据库中.订单包括订单行,每个订单行都可以具有许可证.OrderLine在少数情况下是一对多的,所以我必须做一对多.如果一个OrderLine和许可证用于一个零件,则下一个OrderLine和许可证用于该零件的维护.零件许可证必须链接到维护许可证.

I am trying to enter some data into the database for an Order. The Order includes OrderLines and each OrderLine can have licenses. OrderLine is one to many on rare occasions so I have to do one to many. If one OrderLine and License is for a part, the next OrderLine and License is for Maintenance for that part. The part license must link to the maintenance license.

ParentLicenceId是问题.在我的旧版数据库中,一个许可证与另一份属于PartType维护的许可证之间存在关系.因此,如果客户购买了X部件,则他们可能需要对X部件进行1年的维护.因此,X部件的维护许可证将X部件的许可证列为父部件.

ParentLicenceId is the issue. In my legacy db, there is the relationship between one License and another License that is of PartType Maintenance. So if a customer buys Part X, they might by 1 year of maintenance for Part X. So the Part X Maintenance License will list the Part X license as it's 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();
        }
    }
}

在omc.SaveChanges()处出现错误消息,并且是这样的:

There error message occurs at omc.SaveChanges() and is this:

无法确定'OrderExampleModel.LicenseParentLicense'关系的主要结尾.添加的多个实体可能具有相同的主键.

Unable to determine the principal end of the 'OrderExampleModel.LicenseParentLicense' relationship. Multiple added entities may have the same primary key.

我尝试过:

  1. 添加的多个实体可能具有相同的主键,但是我已经在使用object属性而不是id属性.
  1. Multiple added entities may have the same primary key but I am already using the object property and not the id property.

我阅读了其他几篇文章.我希望只有一些高级设置需要应用.

I've gone through several other articles. I'm hoping there is just some advanced setting I need to apply.

如何克服此错误来保存这些更改?

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

推荐答案

该错误是由在附加实体中检测到的临时非唯一主键引起的.您需要为订单行分配临时主键和外键.

The error is caused by temporary non unique primary keys detected in the attached entities. You need to assign temporary primary keys and foreign keys for order lines.

创建新实体时,实体框架定义了临时实体键并将IsTemporary属性设置为true.当您致电使用SaveChanges方法时,实体框架会分配一个永久密钥,将IsTemporary属性设置为false.- MSDN

默认情况下, orderLine1 orderLine2 具有相同的临时主键,即0(整数的默认值),您需要将它们设置为临时,以防止2个订单行相同的临时ID(即0).

By default orderLine1 and orderLine2 have the same temporary primary keys that are 0 (default value of integer), you need to set them temporary to prevent 2 order lines have the same temporary id (that is 0).

orderLine1.OrderLineId = 1;
orderLine1License.OrderLineId = 1;

orderLine2.OrderLineId = 2;
orderLine2License.OrderLineId = 2;

这篇关于无法确定保存订单时关系的主要终点-EF6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆