Question

I'm using a List object to contain items from a secondary table that my primary table has a one-to-many relationship with. The PK of the primary table serves as the FK field on the secondary table.

However, I'm getting this error when the record attempts to save

Violation of PRIMARY KEY constraint 'PK_ClearinghousePartners'. Cannot insert duplicate 
key in object 'dbo.ClearinghousePartners'. The duplicate key value is (0)

Primary Table Model

FYI - this table contains a LARGE number of fields so I'm just showing the List

public partial class AgentTransmission
{ 
    .
    .
    public virtual List<ClearinghousePartners> ClearinghousePartners { get; set; }
}

Secondary Table Model

public partial class ClearinghousePartners
{
    public int Id { get; set; }
    public string ClearingHouseName { get; set; }
    public string TradingPartnerName { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }
    public int AgtTransId { get; set; } //FK field corresponds to 'Id' on AgentTransmission

    public virtual AgentTransmission AgentTransmission { get; set; }
}

Controller

When the model posts back to the controller to save the exception is thrown in this area of the code. This is an instance in which the AgentTransmission object is completely new and needs to be added to the database. Same goes for each item in the ClearinhousePartners collection.

Each are brand new ClearinghousePartners items with no values for Id and AgtTransId fields. I need the AgentTransmission object to save first so it's Id field can be created and THEN inserted into the AgtTransId field in the ClearinghousePartners objects.

agenttransmission.LastChangeDate = DateTime.Now;
agenttransmission.LastChangeOperator = Security.GetUserName(User);
db.AgentTransmission.Add(agenttransmission);
db.SaveChanges(); //Exception thrown here

View

<fieldset id="ClearinghousePartners">
    <legend>Clearinghouse Partners</legend>
    <center>
        <table>
            <thead>
                <th>Clearinghouse Name</th>
                <th>Trading Partner Name</th>
                <th>Start Date</th>                             
            </thead>
            <tbody>
                @for (int i = 0; i < Model.ClearinghousePartners.Count(); i++)
                {
                    <tr align="center">
                        @Html.HiddenFor(model => model.ClearinghousePartners[i].Id)
                        @Html.HiddenFor(model => model.ClearinghousePartners[i].AgtTransId)
                        <td>@Html.TextBoxFor(model => model.ClearinghousePartners[i].ClearingHouseName, new { style = "width: 100px" })</td>
                        <td>@Html.TextBoxFor(model => model.ClearinghousePartners[i].TradingPartnerName, new { style = "width: 100px" })</td>
                        <td>@Html.TextBoxFor(model => model.ClearinghousePartners[i].StartDate, new { style = "width: 100px" })</td>                                    
                    </tr>
                }
            </tbody>
        </table>                            
    </center>
</fieldset>
Was it helpful?

Solution

The duplicate key value is (0)

I'm guessing that your table is not set to automatically populate your primary key value as an identity property. The declaration should look something like this:

ClearinghousePartners int primary key PK_ClearinghousePartners identity

OTHER TIPS

You need to specify a new unused value for ClearinghousePartners.[PrimaryKey], or make this column an auto-generated column (referred to as an Identity column in sql server - What RDBMS Are you usng?)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top