gpt4 book ai didi

sql-server - 元数据集合中不存在标识为 'component_type_name' 的成员。\r\n参数名称 : identity

转载 作者:行者123 更新时间:2023-12-04 00:07:39 25 4
gpt4 key购买 nike

在数据库中,我有一个名为 SERVICE 的表。对于这个表,我在 INSER/UPDATE/DELETE 之后有触发器。当我尝试使用 EF 将记录插入数据库时​​,我收到错误 “元数据集合中不存在身份为 'component_type_name' 的成员。\r\n参数名称:身份”。 SERVICE 表中不存在“component_type_name”列,但在 COMPONENT_TYPES_IN_SERVICE 表中。这是表 SERVICE 的插入触发器。当我从表中删除触发器时,插入时没有任何问题。

CREATE TRIGGER [dbo].[UpdateComponentTypesInServiceOnInsert]
ON [dbo].[SERVICE]
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

DECLARE @componentID int;
SET @componentID = 0;

DECLARE @ComponentTypeID int;
SET @ComponentTypeID = 0;

DECLARE @ComponentTypeName nvarchar(255);
SET @ComponentTypeName = null;

SELECT @componentID = component_id from inserted;

SELECT @ComponentTypeID = c.component_type_id, @ComponentTypeName = ct.description from COMPONENTS c
inner join dbo.COMPONENT_TYPES ct on c.component_type_id = ct.id
WHERE c.id = @componentID;

Select * from COMPONENT_TYPES_IN_SERVICE cts
where cts.id = @ComponentTypeID;

IF (@@ROWCOUNT > 0)
BEGIN
UPDATE COMPONENT_TYPES_IN_SERVICE
SET number_of_components = number_of_components + 1
WHERE id = @ComponentTypeID;
END
ELSE
BEGIN
INSERT INTO COMPONENT_TYPES_IN_SERVICE(id, component_type_name, number_of_components)
VALUES (@ComponentTypeID, @ComponentTypeName, 1);
END
END

有人知道解决办法吗???

最佳答案

你不能这样做:

Select * from COMPONENT_TYPES_IN_SERVICE cts
where cts.id = @ComponentTypeID;

它将 COMPONENT_TYPES_IN_SERVICE 记录返回给您的应用程序,并且 EF 尝试将返回的值复制到 SERVICE 中,因为它认为您正在传回一些数据库生成的值。

关于sql-server - 元数据集合中不存在标识为 'component_type_name' 的成员。\r\n参数名称 : identity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6238387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com