Roomというデータベーステーブルがあります。
このテーブルには、特定の列にいくつかのデフォルトがあります。
ALTER TABLE [dbo].[Room] ADD CONSTRAINT [DF_Room_Created] DEFAULT (getdate()) FOR [Created]
GO
ALTER TABLE [dbo].[Room] ADD CONSTRAINT [DF_Room_Updated] DEFAULT (getdate()) FOR [Updated]
GO
ALTER TABLE [dbo].[Room] ADD CONSTRAINT [DF_Room_RecordStatus] DEFAULT (0) FOR [RecordStatus]
このテーブルには、次のトリガーもあります。
ALTER TRIGGER [dbo].[RoomInsert] ON [dbo].[Room]
FOR INSERT
AS
DECLARE @PKey int, @TrackingId int
SELECT @PKey = PKey, @TrackingId = TrackingId FROM INSERTED
IF @TrackingId = 0
UPDATE Room
SET TrackingId = @PKey
WHERE PKey = @PKey
現在、EntityFramework4を使用するMVC3Webアプリケーションを開発しています。次のコードを実行すると、デフォルトは適用されず、トリガーは起動されません。
// Create the new room record
Room newRoom = new Room();
newRoom.SiteKey = parentFloor.SiteKey;
newRoom.SiteSurveyKey = parentFloor.SiteSurveyKey;
newRoom.BuildingKey = parentFloor.BuildingKey;
newRoom.FloorKey = parentFloor.PKey;
newRoom.Name = eventModel.RoomName;
newRoom.Description = eventModel.RoomName;
newRoom.CreatedBy = eventModel.UserKey;
_entities.Rooms.AddObject(newRoom);
_entities.SaveChanges();
データベースでプロファイルを実行しましたが、挿入により次のようになります。
exec sp_executesql N'insert [dbo].[Room]([TrackingID], [OriginalPKey], [BuildingKey], [SiteSurveyKey], [SiteKey], [Name], [Description], [RiskColour], [CreatedBy], [Created], [Updated], [RecordStatus], [FloorKey], [DisplayOrder])
values (null, null, @0, @1, @2, @3, @4, null, @5, null, null, null, @6, null)
select [PKey]
from [dbo].[Room]
where @@ROWCOUNT > 0 and [PKey] = scope_identity()',N'@0 int,@1 int,@2 int,@3 varchar(255),@4 varchar(255),@5 int,@6 int',@0=29970,@1=20177,@2=39373,@3='Another Room ',@4='Another Room ',@5=139,@6=25454
NULLが具体的に渡され、sp_executesqlのためにトリガーが起動されないため、デフォルトは適用されないと推測しています。これを回避する方法はありますか?