多补充实体可能会对数据库种子相同的主键 [英] multiple added entities may have the same primary key on database seed

查看:252
本文介绍了多补充实体可能会对数据库种子相同的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我使用有以下模型结构为asp.net的MVC应用5实体框架6

assuming I have the following model structures for an asp.net mvc 5 app using entity framework 6

class Athlete {
 int AthleteID {get; set;}
 List<YearsAsAthlete> YearsAsAthlete {get;set;}
}

class YearsAsAthlete {
 int YearsAsAthleteID {get;set;}
 int AthleteID {get;set;}
 [ForeignKey("AthleteID")]
 Athlete Athlete {get;set;}
 List<ContractRevenue> ContractRevenue {get;set;}
 List<AdvertisementRevenue> AdvertisementRevenue {get;set;}
}

class ContractRevenue {
 int ContractRevenueID {get;set;}
 int YearsAsAthleteID {get;set;}
 [ForeignKey("YearsAsAthleteID")]
 YearsAsAthlete YearsAsAthlete {get;set;}

 List<RevenueAmounts> RevenueAmounts {get;set;}
}

class AdvertisementRevenue {get;set;}
 int AdvertisementRevenueID {get;set;}
 int YearsAsAthleteID {get;set;}
 [ForeignKey("YearsAsAthleteID")]
 YearsAsAthlete YearsAsAthlete {get;set;}

 List<RevenueAmounts> RevenueAmounts {get;set;}
}

class RevenueAmounts {
 int RevenueAmountsID {get;set;}
 int AmountPaid {get;set;}
 date DateOfPayment {get;set;}
}

这些模型做工精细,当我让他们这样,他们有关系,一切都是美味就像一个热软糖圣代。当我运行这一点,数据库中创建这些表和表RevenueAmounts得到2自动生成的外键列ContracRevenue和AdvertisementRevenue。

These models work fine when I have them like this, they have relationships and everything is delicious like a hot fudge sundae. When I run this, the database creates these tables and the RevenueAmounts table get 2 auto-generated foreign key columns for ContracRevenue and AdvertisementRevenue.

不过,我不希望这些为他们奇怪(ContractRevenue_ContractRevenueID)命名,我需要一些方法来访问foreginkey id属性在我的岗位控制器方法添加与正确类型的收入相关新值。

However, I don't want these as they're named strangely (ContractRevenue_ContractRevenueID) and I need some way to access the foreginkey id property in my post controller method for adding new values that correlate with the right type of revenue.

当我改变RevenueAmounts模型如下:

When I change the RevenueAmounts model to the following:

class RevenueAmounts {
 int RevenueAmountsID {get;set;}
 int AmountPaid {get;set;}
 date DateOfPayment {get;set;}

 // ***NOTE adding foreign keys here

 int ContractRevenueID {get;set;}
 [ForeginKey("ContractRevenueID")]
 ContractRevenue ContractRevenue {get;set;}

 int AdvertisementRevenueID {get;set;}
 [ForeignKey("AdvertisementRevenueID")]
 AdvertisementRevenue AdvertisementRevenue {get;set;}
}

我开始变得异常:

I start getting an exception:

[SQLEXCEPTION(0x80131904):在介绍表'AdvertisementRevenueFOREIGN KEY约束'FK_dbo.AdvertisementRevenue_dbo.YearsAsAthlete_YearsAsAthleteID'可能会导致循环或多重级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

[SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_dbo.AdvertisementRevenue_dbo.YearsAsAthlete_YearsAsAthleteID' on table 'AdvertisementRevenue' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

** 修改 **

** EDIT **

我已经关掉了级联使用流利的API但现在我得到一个不同的异常删除的功能:

I've turned off the cascading delete functionality using the fluent API however now I'm getting a different exception:

无法确定GIP.DAL.ContractRevenue_RevenueAmounts关系的主要终点。多个添加实体可以具有相同的主密钥

Unable to determine the principal end of the 'GIP.DAL.ContractRevenue_RevenueAmounts' relationship. Multiple added entities may have the same primary key.

顺便说一句,除了正在当我试图种子一堆信息到数据库,然后做 context.SaveChanges()在底部抛出(仅做一次最后)

By the way, the exception is being thrown when I'm trying to seed a bunch of info into the database and then doing context.SaveChanges() at the bottom (only doing it once at the end)

推荐答案

在你的编辑,你提到的多添加实体可能具有相同的主键。错误。不知道所有的你在做什么这里的细节,听起来好像你正在创建与实体的关系 - 其中有两个具有相同ID的上下文。这些可能是尚未被保存这是他们从数据库获得一个自动生成的ID的新实体。如果关系是基于该ID那么有一些模糊因为实体框架是无法确定的关系实际上是指向新的实体 - 它们都具有该关系指向该ID

In your edit, you mention the 'Multiple added entities may have the same primary key.' error. Without knowing all of the details of what you are doing here, it sounds like you are creating a relationship with an entity - of which there are two in the context with the same ID. These are probably new entities which have not yet been saved which is where they get an automatically generated ID from the database. If the relationship is based on the ID then there is some ambiguity because Entity Framework is unable to determine which of the new entities the relationship is actually pointing to - they both have the ID that the relationship is pointing to.

有两种可能的修正。


  1. 生成对于实体的临时,唯一的标识符,因为它们是在上下文创建。实体框架将放弃它作为实体保存,但直到这一点,就可以用它来从另一个区分一种新的实体。我已经使用负整数为此在过去。

  1. Generate a temporary, unique identifier for entities as they are created in the context. Entity Framework will discard this as the entity is saved but up until that point, it can use it to tell one new entity apart from the other. I have used negative integers for this purpose in the past.

不要创建一个使用的ID,而是对实体引用的关系。如果实体框架有直接的参考实体,那么它并不需要经过识别基于非唯一标识符的实体的过程中,不应该有这个问题。

Do not create the relationships using IDs but rather on entity references. If Entity Framework has a direct reference to the entity, then it does not need to go through the process of identifying the entity based on non-unique identifiers and should not have this problem.

这篇关于多补充实体可能会对数据库种子相同的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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