Question

I have a set of tables (ConditionTemplate and KeyWord) that have a many to many relationship. In code I am trying to add a Keyword to a specific ConditionTemplate record. Unfortunately, when I think I'm adding a Keyword to a specific condition I'm getting an error as if it's adding a new Keyword without being associated to a condition.

An Image of my Model: enter image description here

My Code:

Global Variables Creation:

    EnterpriseEntities EE;
    ConditionTemplate myConditionTemplate;

Load Global Variables:

            EE = new EnterpriseEntities();
            EE.Database.Connection.ConnectionString = Myapp.EnterpriseEntityConnectionString;

            myConditionTemplate = EE.ConditionTemplates.Where(c => c.TemplateCode == "17D").FirstOrDefault();

The above code loads a single Condition with Many Keywords.

Available Keywords are in a listbox and the user pushed a button to select a keyword(s) to move to the condition. This is the code that handles that.

                foreach (KeyWord SelectedKeyWord in ListBoxAvailableKeyWords.SelectedItems)
                {



                    KeyWord NewKeyWord = new KeyWord
                    {
                        KeyWordID = SelectedKeyWord.KeyWordID,
                        ID = SelectedKeyWord.ID,
                        Word = SelectedKeyWord.Word

                    };

                    myConditionTemplate.KeyWords.Add(NewKeyWord);



                }

Then the user pushes a button to save changes and I call

 EE.SaveChanges

Then I get this error:

System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Violation of UNIQUE KEY constraint 'IX_KeyWord'. Cannot insert duplicate key in object 'dbo.KeyWord'. The duplicate key value is (ADJUDICATION). The statement has been terminated.

If I remove the code that sets the word property (Word = SelectedKeyWord.Word ) when I create the keyword object I get this error.

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Which tells me the word field is required.

Was it helpful?

Solution

In order to tell EF that the KeyWords you selected already exist in the database and to avoid the problem you must attach them to the context:

foreach (KeyWord SelectedKeyWord in ListBoxAvailableKeyWords.SelectedItems)
{
    KeyWord NewKeyWord = new KeyWord
    {
        // You actually only need to set the primary key property here
        ID = SelectedKeyWord.ID
    };

    EE.KeyWords.Attach(NewKeyWord);
    myConditionTemplate.KeyWords.Add(NewKeyWord);
}

Edit

If the KeyWord entities are already attached to your context (because they have been loaded before with the same context for example) you can use instead:

foreach (KeyWord SelectedKeyWord in ListBoxAvailableKeyWords.SelectedItems)
{
    KeyWord NewKeyWord = EE.KeyWords.Find(SelectedKeyWord.ID);
    myConditionTemplate.KeyWords.Add(NewKeyWord);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top