实体框架,外键和EntityKeys

时间:2021-12-04 11:29:21

I've got a problem with the Entity Framework and the ForeignKeys. I've got a table "BC_Message_Assets" which has 3 FK's (MessageId, AssetId and StatusId). I create my "MessageAsset" like this

我遇到了Entity Framework和ForeignKeys的问题。我有一个表“BC_Message_Assets”,它有3个FK(MessageId,AssetId和StatusId)。我像这样创建我的“MessageAsset”

MessageAsset messageAsset = new MessageAsset();

messageAsset.MessageStatusReference.EntityKey = new EntityKey("MyEntities.MessageStatusSet", "Id", 1);

messageAsset.AssetReference.EntityKey = new EntityKey("MyEntities.AssetSet", "Id", 1);

messageAsset.MessageReference.EntityKey = new EntityKey("MyEntities.MessageSet", "Id", messageId);

context.AddToMessageAssetSet(messageAsset);
context.SaveChanges();

But I got the following exception :

但我得到以下例外:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_BC_Message_Assets_BC_Assets". The conflict occurred in database "Test", table "dbo.BC_Assets", column 'Id'. The statement has been terminated.

INSERT语句与FOREIGN KEY约束“FK_BC_Message_Assets_BC_Assets”冲突。冲突发生在数据库“Test”,表“dbo.BC_Assets”,列“Id”中。该语句已终止。

When I look at the query, I notice that the parameter value for AssetId is "0" despite that I provided "1" to the EntityKey. Here's the generated query :

当我查看查询时,我注意到AssetId的参数值为“0”,尽管我向EntityKey提供了“1”。这是生成的查询:

exec sp_executesql N'insert dbo.[BC_Message_Assets]([MessageId], [AssetId], [CompletionTime], [StatusId]) values (@0, @1, null, @2) ',N'@0 int,@1 int,@2 int',@0=47,@1=0,@2=1

I can't explain what happens. I hardcoded "1" in the EntityKey and I received "0" in the query ?

我无法解释会发生什么。我在EntityKey中硬编码“1”,我在查询中收到“0”?

2 个解决方案

#1


1  

I got the problem with my unit tests.

我的单元测试遇到了问题。

But I found cause of the problem. The problem was that the PK on my BC_Message_Assets table was based on the 2 FK (MessageId and AssetId). With a simple identifier (int + identity), it works without problem ... Strange !

但我找到了问题的原因。问题是我的BC_Message_Assets表上的PK基于2 FK(MessageId和AssetId)。使用简单的标识符(int + identity),它可以正常工作......奇怪!

#2


0  

As you say you programmed 1,1,1 but got 47,0,1

如你所说,你编程1,1,1但得到了47,0,1

It was 2 of the values that were not expected.

这是两个未预料到的值。

One thing that could be happening is that there is some other code that is generating this row. When you save it saves the row with the error first, this throws and exception, and you never see the row that you created.

可能发生的一件事是,有一些其他代码正在生成此行。保存时,首先保存包含错误的行,此抛出和异常,并且您永远不会看到您创建的行。

Try writing a unit test with only the code in your question.

尝试仅使用问题中的代码编写单元测试。

#1


1  

I got the problem with my unit tests.

我的单元测试遇到了问题。

But I found cause of the problem. The problem was that the PK on my BC_Message_Assets table was based on the 2 FK (MessageId and AssetId). With a simple identifier (int + identity), it works without problem ... Strange !

但我找到了问题的原因。问题是我的BC_Message_Assets表上的PK基于2 FK(MessageId和AssetId)。使用简单的标识符(int + identity),它可以正常工作......奇怪!

#2


0  

As you say you programmed 1,1,1 but got 47,0,1

如你所说,你编程1,1,1但得到了47,0,1

It was 2 of the values that were not expected.

这是两个未预料到的值。

One thing that could be happening is that there is some other code that is generating this row. When you save it saves the row with the error first, this throws and exception, and you never see the row that you created.

可能发生的一件事是,有一些其他代码正在生成此行。保存时,首先保存包含错误的行,此抛出和异常,并且您永远不会看到您创建的行。

Try writing a unit test with only the code in your question.

尝试仅使用问题中的代码编写单元测试。