Search code examples
c#entity-framework-4entitycollection

List to EntityCollection


I've been trying to add some selected items from a multi-select listbox on my entry class.

After some research I found that this solution would work:

EntityCollection<Publisher> entityCollection = new EntityCollection<Publisher>();

foreach (Publisher pub in this.publishersLst.SelectedItems)
{
    entityCollection.Attach(pub);
}

but even though it solved the first problem I was having I'm getting a new one now. One that I can't seem to find the solution... I even tried detaching the entity, but with no luck.

The error I get now is:

Requested operation is not allowed when the owner of this RelatedEnd is null. RelatedEnd objects that were created with the default constructor should only be used as a container during serialization.

have anyone been through this problem?

Thanks.


Solution

  • I solved it a different way.

                entry.Publishers = new EntityCollection<Publisher>();
    
                foreach (Publisher item in this.publishersLst.SelectedItems)
                {
                    entry.Publishers.Add(item);
                }
    

    Needed a new List to work.

    Regards.