在存在触发器的情况下,将行从linqtosql插入sql server时出错
本文关键字:插入 linqtosql sql server 出错 触发器 存在 情况下 | 更新日期: 2023-09-27 18:27:41
我在sql server 中为下表创建了一个触发器
Buildings: ID decimal(24, 0) PK, Name varchar(255)
触发是
CREATE TRIGGER [dbo].[TRG_BLD]
ON [dbo].[Building]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO Building (Name)
SELECT Name
FROM inserted
END
它所做的只是将行插入到表中(它做得更多,但我正在努力简化我的情况)。
当我从sql服务器插入一行时,一切都很好,但是当我通过LinqToSql插入时,
Building b = new Building();
b.Name = "building A";
DC.Buildings.InsertOnSubmit(b);
DC.SubmitChanges();
"SubmitChanges"上出现异常,说明:
System.Data.Linq.dll 中发生类型为"System.InvalidOperationException"的未处理异常
附加信息:不能将null值分配给类型为System.Decimal的成员,该成员是不可为null值的类型。
IF OBJECT_ID('dbo.Building', 'U') IS NOT NULL
DROP TABLE dbo.Building
GO
CREATE TABLE dbo.Building
(
ID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(255)
)
GO
CREATE UNIQUE NONCLUSTERED INDEX ix ON dbo.Building (Name) WITH(IGNORE_DUP_KEY=ON)
GO
CREATE TRIGGER dbo.TRG_BLD
ON dbo.Building
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.Building (Name)
SELECT Name
FROM INSERTED
END
GO
INSERT INTO dbo.Building (Name)
VALUES ('a'), ('a'), ('b'), ('c'), (NULL), (NULL)
GO
SELECT *
FROM dbo.Building
输出-
ID Name
----------- ------------
1 a
3 b
4 c
5 NULL