awj
awj

Reputation: 7949

Entity Framework: Unable to add entry to cross-ref table

In my DB I have the following tables:

st_dictionary
    id
    [other fields]

st_language
    id
    [other fields]

st_dictionary_language
    dictionary_id
    language_id

When I created the Entity Model of these (and other) tables, the Entity Framework appeared intelligent enough to 'condense' the st_dictionary_language cross-referencing table into

public partial class st_dictionary
{
    public virtual ICollection<st_language> st_language { get; set; }
    [other members]
}

As anticipated, this property is populated with the st_language entities which are linked through the cross-referencing table.

However, when I wish to reference another language to this st_dictionary entity I run into a problem:

daoDictionary.st_language.Add(new st_language { id = newLangID });

The above code throws an exception which lists a series of validation errors, each of which explains that some st_language properties cannot be null. These non-null properties all exist in the st_language DB table, not in the cross-referencing table.

So how do I add a new entry in the st_dictionary_language table?

Upvotes: 1

Views: 50

Answers (1)

Remy
Remy

Reputation: 12693

You have to add the actual entity that you wish to cross-reference rather than passing in a new object (which, in your example, you populate with only the id property).

Looking at your example, probably something like this:

var daoLanguage = [retrieve the st_language entity via the Entity Framework];
daoDictionary.st_language.Add(daoLanguage);

Good luck!

Upvotes: 1

Related Questions