2013-03-05 61 views
25

back story: 我們主要使用AWS進行一切(託管,數據庫,通知等)。現在,我正在考慮將數據庫端移到SQL Azure,因爲我們已經在AWS RDS上收到了瘋狂的賬單。所以我試圖做的是在SQL Azure中創建一個數據庫,並更新連接字符串以指向新的數據庫。過去,ELMAH(這個具體實現:https://github.com/alexanderbeletsky/elmah.mvc)過去完美無瑕。MVC ELMAH和SQL Azure

現狀: 我剛剛創建一個新的數據庫在SQL Azure中,發現的主要區別就即關於不支持:

ON [PRIMARY],NONCLUSTERED鑰匙等

我遷移我的數據庫很好(現在),但是當我將ELMAH的更新腳本應用到數據庫並嘗試進入該工具時,出現錯誤!

我莫名其妙地相信,這是一個分貝問題,因爲如果我刪除: <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="DefaultConnection"/>基本上默認ELMAH本地存儲的一切,我得到訪問ELMAH。

有人讓ELMAH使用SQL Azure嗎?你可以給我SQL腳本來生成表和存儲過程嗎?

+1

DefaultConnection字符串是什麼樣子(混淆user/pwd/dbname) – Tommy 2013-03-05 19:08:56

回答

72

這是數據庫腳本我使用的數據塊ELMAH SQL Azure上:

--~Changing index [dbo].[ELMAH_Error].PK_ELMAH_Error to a clustered index. You may want to pick a different index to cluster on. 
SET ANSI_NULLS ON 
SET QUOTED_IDENTIFIER ON 
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo]. [ELMAH_Error]') AND type in (N'U')) 
BEGIN 
CREATE TABLE [dbo].[ELMAH_Error](
[ErrorId] [uniqueidentifier] NOT NULL, 
[Application] [nvarchar](60) NOT NULL, 
[Host] [nvarchar](50) NOT NULL, 
[Type] [nvarchar](100) NOT NULL, 
[Source] [nvarchar](60) NOT NULL, 
[Message] [nvarchar](500) NOT NULL, 
[User] [nvarchar](50) NOT NULL, 
[StatusCode] [int] NOT NULL, 
[TimeUtc] [datetime] NOT NULL, 
[Sequence] [int] IDENTITY(1,1) NOT NULL, 
[AllXml] [nvarchar](max) NOT NULL, 
CONSTRAINT [PK_ELMAH_Error] PRIMARY KEY CLUSTERED 
(
[ErrorId] ASC 
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) 
) 
END 

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[ELMAH_Error]') AND name = N'IX_ELMAH_Error_App_Time_Seq') 
CREATE NONCLUSTERED INDEX [IX_ELMAH_Error_App_Time_Seq] ON [dbo].[ELMAH_Error] 
(
[Application] ASC, 
[TimeUtc] DESC, 
[Sequence] DESC 
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) 
GO 
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id =  OBJECT_ID(N'[DF_ELMAH_Error_ErrorId]') AND type = 'D') 
BEGIN 
ALTER TABLE [dbo].[ELMAH_Error] ADD CONSTRAINT [DF_ELMAH_Error_ErrorId] DEFAULT (newid()) FOR [ErrorId] 
END 

GO 
SET ANSI_NULLS ON 
SET QUOTED_IDENTIFIER ON 
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ELMAH_GetErrorsXml]') AND type in (N'P', N'PC')) 
BEGIN 
EXEC dbo.sp_executesql @statement = N' 
CREATE PROCEDURE [dbo].[ELMAH_GetErrorsXml] 
(
@Application NVARCHAR(60), 
@PageIndex INT = 0, 
@PageSize INT = 15, 
@TotalCount INT OUTPUT 
) 
AS 

SET NOCOUNT ON 

DECLARE @FirstTimeUTC DATETIME 
DECLARE @FirstSequence INT 
DECLARE @StartRow INT 
DECLARE @StartRowIndex INT 

SELECT 
    @TotalCount = COUNT(1) 
FROM 
    [ELMAH_Error] 
WHERE 
    [Application] = @Application 

-- Get the ID of the first error for the requested page 

SET @StartRowIndex = @PageIndex * @PageSize + 1 

IF @StartRowIndex <= @TotalCount 
BEGIN 

    SET ROWCOUNT @StartRowIndex 

    SELECT 
     @FirstTimeUTC = [TimeUtc], 
     @FirstSequence = [Sequence] 
    FROM 
     [ELMAH_Error] 
    WHERE 
     [Application] = @Application 
    ORDER BY 
     [TimeUtc] DESC, 
     [Sequence] DESC 

END 
ELSE 
BEGIN 

    SET @PageSize = 0 

END 

-- Now set the row count to the requested page size and get 
-- all records below it for the pertaining application. 

SET ROWCOUNT @PageSize 

SELECT 
    errorId  = [ErrorId], 
    application = [Application], 
    host  = [Host], 
    type  = [Type], 
    source  = [Source], 
    message  = [Message], 
    [user]  = [User], 
    statusCode = [StatusCode], 
    time  = CONVERT(VARCHAR(50), [TimeUtc], 126) + ''Z'' 
FROM 
    [ELMAH_Error] error 
WHERE 
    [Application] = @Application 
AND 
    [TimeUtc] <= @FirstTimeUTC 
AND 
    [Sequence] <= @FirstSequence 
ORDER BY 
    [TimeUtc] DESC, 
    [Sequence] DESC 
FOR 
    XML AUTO 

' 
END 
GO 
SET ANSI_NULLS ON 
SET QUOTED_IDENTIFIER ON 
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ELMAH_GetErrorXml]') AND type in (N'P', N'PC')) 
BEGIN 
EXEC dbo.sp_executesql @statement = N' 
CREATE PROCEDURE [dbo].[ELMAH_GetErrorXml] 
(
@Application NVARCHAR(60), 
@ErrorId UNIQUEIDENTIFIER 
) 
AS 

SET NOCOUNT ON 

SELECT 
    [AllXml] 
FROM 
    [ELMAH_Error] 
WHERE 
    [ErrorId] = @ErrorId 
AND 
    [Application] = @Application 

' 
END 
GO 
SET ANSI_NULLS ON 
SET QUOTED_IDENTIFIER ON 
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo]. [ELMAH_LogError]') AND type in (N'P', N'PC')) 
BEGIN 
EXEC dbo.sp_executesql @statement = N' 
CREATE PROCEDURE [dbo].[ELMAH_LogError] 
(
@ErrorId UNIQUEIDENTIFIER, 
@Application NVARCHAR(60), 
@Host NVARCHAR(30), 
@Type NVARCHAR(100), 
@Source NVARCHAR(60), 
@Message NVARCHAR(500), 
@User NVARCHAR(50), 
@AllXml NVARCHAR(MAX), 
@StatusCode INT, 
@TimeUtc DATETIME 
) 
AS 

SET NOCOUNT ON 

INSERT 
INTO 
    [ELMAH_Error] 
    (
     [ErrorId], 
     [Application], 
     [Host], 
     [Type], 
     [Source], 
     [Message], 
     [User], 
     [AllXml], 
     [StatusCode], 
     [TimeUtc] 
    ) 
VALUES 
    (
     @ErrorId, 
     @Application, 
     @Host, 
     @Type, 
     @Source, 
     @Message, 
     @User, 
     @AllXml, 
     @StatusCode, 
     @TimeUtc 
    ) 

' 
END 
GO 

SQL Azure是SQL的一個特殊版本,有一些東西,它不會支持。

你使用SQL Azure Migrate Wizard遷移你的數據庫嗎?

+0

是的,我嘗試使用該工具。在你的SQL腳本中,我有同一個,但無論如何嘗試,它stil不起作用。 :( 我真的確定它與數據庫,因爲如果我刪除ELMAH的連接字符串(這意味着它存儲日誌本地),我進入接口 你有一個單獨的連接字符串單獨的數據庫ELMAH?過去有一個單獨的數據庫,但現在我只是使用默認連接 – AnimaSola 2013-03-05 16:58:48

+0

您試過了香草版本的ELMAH嗎?這可能是實現特定的。 – 2013-03-06 15:30:26

+1

我在使用Azure上的elmah時遇到問題,您的SQL腳本修復了我的問題 – Naz 2013-04-01 15:09:41