如何创建 ELMAH SQL Server 数据库?

发布于 2024-12-15 05:12:41 字数 84 浏览 2 评论 0原文

如何创建 ELMAH SQL Server 数据库?我通过 NuGet 将其添加到我的 ASP.NET MVC 项目中,但我的计算机上没有 sql 脚本。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的鱼塘能养鲲 2024-12-22 05:12:41

DDL 脚本可从 Elmah 下载页面 链接。无需搜寻源代码树。

(我不明白为什么它没有与 NuGet 捆绑在一起)

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

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

蓝眼睛不忧郁 2024-12-22 05:12:41

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

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

I found the script in source control: 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.

一念一轮回 2024-12-22 05:12:41

对于代码优先迁移场景,我发现这篇文章非常有帮助。

首先在“Package Manager Console”中运行 Add-Migration AddElmah 命令。这将在 Migration 文件夹下创建一个文件。该文件将包含 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");
}

现在,当您在“Package Manager Console”中执行 Update-Database 命令时,ELMAH_Error 表和关联过程将在数据库中创建。

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:

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.

清泪尽 2024-12-22 05:12:41

Elmah 源已移至 GitHub。 SQL Server 的当前文件位于 Elmah 组织下的 SqlErrorLog 存储库中。
SqlErrorLog: /src/SQLServer.sql

其他数据库脚本可以通过在 Elmah GitHub 组织下搜索 errorlog 可以找到。例如 https://github.com/elmah?q=errorlog

Elmah source has been moved to GitHub. The current file for SQL Server is located in the SqlErrorLog repo under the Elmah organization.
SqlErrorLog: /src/SQLServer.sql

Other database scripts can be found by searching errorlog under the Elmah GitHub organization. e.g. https://github.com/elmah?q=errorlog

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文