如何创建ELMAH SQL服务器数据库?

时间:2022-01-02 07:24:27

How do I create the ELMAH SQL Server database? I added it to my ASP.NET MVC project through NuGet and don't have the sql script on my machine.

如何创建ELMAH SQL服务器数据库?我把它添加到ASP中。NET MVC项目通过NuGet,我的机器上没有sql脚本。

3 个解决方案

#1


29  

The DDL script is linked from the Elmah downloads page. No need to trawl the source tree.

DDL脚本从Elmah下载页面链接。不需要拖拽源树。

(Why it's not bundled with the NuGet escapes me)

(为什么它没有和NuGet escape绑定在一起?)

#2


23  

I found the script in source control: https://code.google.com/p/elmah/source/browse/src/Elmah.SqlServer/SQLServer.sql

我在源代码控制中找到了脚本:https://code.google.com/p/elmah/source/browse/src/Elmah.SqlServer/SQLServer.sql。

Run that script on the database you are using to create the database structures for Elmah.

在要为Elmah创建数据库结构的数据库上运行该脚本。

#3


0  

For a code first migration scenario, I found the this article very helpful.

对于代码优先迁移场景,我发现本文非常有用。

First run Add-Migration AddElmah command in 'Package Manager Console'. This will create a file under Migration folder. This file will contain AddElmah class with to functions Up() and Down(). Replaced these two functions with below code:

首先在“包管理控制台”中运行Add-Migration AddElmah命令。这将在迁移文件夹下创建一个文件。该文件将包含AddElmah类,其中包含函数Up()和Down()。以下代码替换这两个函数:

public override void Up()
{
    Sql(@"CREATE TABLE [dbo].[ELMAH_Error]
        (
            [ErrorId]     UNIQUEIDENTIFIER NOT NULL,
            [Application] NVARCHAR(60)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [Host]        NVARCHAR(50)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [Type]        NVARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [Source]      NVARCHAR(60)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [Message]     NVARCHAR(500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [User]        NVARCHAR(50)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [StatusCode]  INT NOT NULL,
            [TimeUtc]     DATETIME NOT NULL,
            [Sequence]    INT IDENTITY(1, 1) NOT NULL,
            [AllXml]      NTEXT COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
        ) ");

    Sql("EXEC('ALTER TABLE [dbo].[ELMAH_Error] WITH NOCHECK ADD CONSTRAINT[PK_ELMAH_Error] PRIMARY KEY([ErrorId])')");

    Sql("EXEC('ALTER TABLE [dbo].[ELMAH_Error] ADD CONSTRAINT[DF_ELMAH_Error_ErrorId] DEFAULT(NEWID()) FOR[ErrorId]')");

    Sql(@"EXEC('CREATE NONCLUSTERED INDEX [IX_ELMAH_Error_App_Time_Seq] ON [dbo].[ELMAH_Error] 
        (
            [Application]   ASC,
            [TimeUtc]       DESC,
            [Sequence]      DESC
        )')");

    Sql(@"EXEC('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')");

    Sql(@"EXEC('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

            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

            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')");

    Sql(@"EXEC('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 NTEXT, @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)')");
}

public override void Down()
{
    Sql("EXEC('DROP PROCEDURE [ELMAH_GetErrorXml]')");
    Sql("EXEC('DROP PROCEDURE [ELMAH_GetErrorsXml]')");
    Sql("EXEC('DROP PROCEDURE [ELMAH_LogError]')");
    Sql("Drop table ELMAH_Error");
}

Now when you will execute Update-Database command in "Package Manager Console", ELMAH_Error table and associated procedures will be created in the database.

现在,当您在“包管理器控制台”中执行Update-Database命令时,将在数据库中创建ELMAH_Error表和相关的过程。

#1


29  

The DDL script is linked from the Elmah downloads page. No need to trawl the source tree.

DDL脚本从Elmah下载页面链接。不需要拖拽源树。

(Why it's not bundled with the NuGet escapes me)

(为什么它没有和NuGet escape绑定在一起?)

#2


23  

I found the script in source control: https://code.google.com/p/elmah/source/browse/src/Elmah.SqlServer/SQLServer.sql

我在源代码控制中找到了脚本:https://code.google.com/p/elmah/source/browse/src/Elmah.SqlServer/SQLServer.sql。

Run that script on the database you are using to create the database structures for Elmah.

在要为Elmah创建数据库结构的数据库上运行该脚本。

#3


0  

For a code first migration scenario, I found the this article very helpful.

对于代码优先迁移场景,我发现本文非常有用。

First run Add-Migration AddElmah command in 'Package Manager Console'. This will create a file under Migration folder. This file will contain AddElmah class with to functions Up() and Down(). Replaced these two functions with below code:

首先在“包管理控制台”中运行Add-Migration AddElmah命令。这将在迁移文件夹下创建一个文件。该文件将包含AddElmah类,其中包含函数Up()和Down()。以下代码替换这两个函数:

public override void Up()
{
    Sql(@"CREATE TABLE [dbo].[ELMAH_Error]
        (
            [ErrorId]     UNIQUEIDENTIFIER NOT NULL,
            [Application] NVARCHAR(60)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [Host]        NVARCHAR(50)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [Type]        NVARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [Source]      NVARCHAR(60)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [Message]     NVARCHAR(500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [User]        NVARCHAR(50)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
            [StatusCode]  INT NOT NULL,
            [TimeUtc]     DATETIME NOT NULL,
            [Sequence]    INT IDENTITY(1, 1) NOT NULL,
            [AllXml]      NTEXT COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
        ) ");

    Sql("EXEC('ALTER TABLE [dbo].[ELMAH_Error] WITH NOCHECK ADD CONSTRAINT[PK_ELMAH_Error] PRIMARY KEY([ErrorId])')");

    Sql("EXEC('ALTER TABLE [dbo].[ELMAH_Error] ADD CONSTRAINT[DF_ELMAH_Error_ErrorId] DEFAULT(NEWID()) FOR[ErrorId]')");

    Sql(@"EXEC('CREATE NONCLUSTERED INDEX [IX_ELMAH_Error_App_Time_Seq] ON [dbo].[ELMAH_Error] 
        (
            [Application]   ASC,
            [TimeUtc]       DESC,
            [Sequence]      DESC
        )')");

    Sql(@"EXEC('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')");

    Sql(@"EXEC('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

            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

            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')");

    Sql(@"EXEC('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 NTEXT, @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)')");
}

public override void Down()
{
    Sql("EXEC('DROP PROCEDURE [ELMAH_GetErrorXml]')");
    Sql("EXEC('DROP PROCEDURE [ELMAH_GetErrorsXml]')");
    Sql("EXEC('DROP PROCEDURE [ELMAH_LogError]')");
    Sql("Drop table ELMAH_Error");
}

Now when you will execute Update-Database command in "Package Manager Console", ELMAH_Error table and associated procedures will be created in the database.

现在,当您在“包管理器控制台”中执行Update-Database命令时,将在数据库中创建ELMAH_Error表和相关的过程。