Skip to content

[API Proposal]: Specialized Sql Exception Type for Duplicate Key Values #73779

Open
@TonyValenti

Description

@TonyValenti

Background and motivation

When a duplicate key value interrupts a SQL command, you get a SqlException with a message like this:

 ---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert duplicate key row in object 'dbo.__M_Matters' with unique index 'IX___M_Matters_Id'. The duplicate key value is (SB2 --- Accounts Payable).

It would be really nice if the duplicate key value was available as a property.

API Proposal

namespace Microsoft.Data.SqlClient;

public class DuplicateKeyException : SqlException
{
  public string Key {get; set;}
}

API Usage

It is a new exception that would be thrown.

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions