无效的对象名称“插入”在触发器中

问题描述 投票:0回答:0

我正在使用 EF Core 并为付款表定义了以下有效触发器,但是当我使用我的应用程序时,在插入新付款时上下文在保存时返回错误:

Microsoft.EntityFrameworkCore.更新:
错误:保存上下文类型“ApplicationDbContext”的更改时数据库发生异常。
Microsoft.EntityFrameworkCore.DbUpdateException:保存实体更改时出错。有关详细信息,请参阅内部异常。

Microsoft.Data.SqlClient.SqlException (0x80131904): 无效的对象名称 'MRPayments.inserted'

这是触发代码:

CREATE TRIGGER trg_Payment_Log
ON MRPayments.Payments
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
    DECLARE @Action nvarchar(10)

    IF EXISTS (SELECT * FROM [MRPayments].[inserted])
    BEGIN
        IF EXISTS (SELECT * FROM [MRPayments].[deleted])
            SET @Action = 'UPDATE'
        ELSE
            SET @Action = 'INSERT'
    END
    ELSE
        SET @Action = 'DELETE'

    INSERT INTO MRLogs.Payment_Logs (PaymentId, Action, OldValues, NewValues, LogDate)
    SELECT 
        CASE 
            WHEN @Action = 'DELETE' THEN [MRPayments].[deleted].Id 
            ELSE [MRPayments].[inserted].Id 
        END,
        @Action,
        CASE 
            WHEN @Action = 'UPDATE' THEN dbo.GetOldValues_Payment([MRPayments].[deleted].Id) 
            ELSE NULL 
        END,
        CASE 
            WHEN @Action = 'INSERT' THEN dbo.GetNewValues_Payment([MRPayments].[inserted].Id) 
            ELSE dbo.GetNewValues_Payment([MRPayments].[deleted].Id) 
        END,
        GETDATE()
    FROM [MRPayments].[inserted]
    FULL OUTER JOIN [MRPayments].[deleted] ON [MRPayments].[inserted].Id = [MRPayments].[deleted].Id
END
GO

我对模式表名使用完全限定符,但当我只使用

inserted
而不是
[MRPayments].[inserted]
时,会发生同样的错误。我在
applicationdbcontext
看到需要澄清,但是之后又出现同样的错误:

  modelBuilder
        .Entity<Payment>()
        .ToTable(t => t.HasTrigger("trg_Payment_Log"));

这是具有功能和插入和删除功能的完整代码,而不是评论部分提到的 [MRPayments].[inserted] 和 [MRPayments].[deleted]。但它仍然无法正常工作:

CREATE TRIGGER trg_Payment_Log
ON MRPayments.Payments
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
    DECLARE @Action nvarchar(10)

    IF EXISTS (SELECT * FROM  inserted)
    BEGIN
        IF EXISTS (SELECT * FROM deleted)
            SET @Action = 'UPDATE'
        ELSE
            SET @Action = 'INSERT'
    END
    ELSE
        SET @Action = 'DELETE'

    INSERT INTO MRLogs.Payment_Logs (PaymentId, Action, OldValues, NewValues, LogDate)
    SELECT 
        CASE 
            WHEN @Action = 'DELETE' THEN deleted.Id 
            ELSE inserted.Id 
        END,
        @Action,
        CASE 
            WHEN @Action = 'UPDATE' THEN dbo.GetOldValues_Payment(deleted.Id) 
            ELSE NULL 
        END,
        CASE 
            WHEN @Action = 'INSERT' THEN dbo.GetNewValues_Payment(inserted.Id) 
            ELSE dbo.GetNewValues_Payment(deleted.Id) 
        END,
        GETDATE()
    FROM inserted
    FULL OUTER JOIN deleted ON inserted.Id = deleted.Id
END

GO

CREATE FUNCTION dbo.GetOldValues_Payment(@PaymentId uniqueidentifier)
RETURNS nvarchar(max)
AS
BEGIN
    DECLARE @OldValues nvarchar(max)

    SELECT @OldValues = 
        'PaymentStatus: ' + COALESCE(CONVERT(nvarchar(max), deleted.PaymentStatus), 'NULL') + CHAR(13) + CHAR(10) +
        'ApplicationUserId: ' + COALESCE(CONVERT(nvarchar(max), deleted.ApplicationUserId), 'NULL') + CHAR(13) + CHAR(10) +
        'PaymentValuePLN: ' + COALESCE(CONVERT(nvarchar(max), deleted.PaymentValuePLN), 'NULL') + CHAR(13) + CHAR(10)
    FROM deleted
    WHERE deleted.Id = @PaymentId

    RETURN @OldValues
END

GO

CREATE FUNCTION GetNewValues_Payment (@PaymentId uniqueidentifier)
RETURNS nvarchar(max)
AS
BEGIN
    DECLARE @NewValues nvarchar(max)

    SELECT @NewValues = CONCAT(
        'PaymentStatus: ', COALESCE(NEW.PaymentStatus, 'NULL'), CHAR(13), CHAR(10),
        'ApplicationUserId: ', COALESCE(NEW.ApplicationUserId, 'NULL'), CHAR(13), CHAR(10),
        'PaymentValuePLN: ', COALESCE(CAST(NEW.PaymentValuePLN AS nvarchar(max)), 'NULL')
    )
    FROM inserted AS NEW
    WHERE NEW.Id = @PaymentId

    RETURN @NewValues
END

Go
c# sql sql-server entity-framework triggers
© www.soinside.com 2019 - 2024. All rights reserved.