c# - 'Sequence contains more than one element' (InvalidOperationException) thrown by external System.Core call when using Linq2Sql -


firstly let me explain understand why invalidoperationexception has been thrown , looking way avoid being thrown. there call system.linq.enumerable.singleordefault() can see in visual studio 2010 call stack window. however, call in external linq2sql code, have no access change it.

call stack showing (mostly) external code. (sorry, may need zoom in see image properly)

the last internal code call before execution goes external found in application dbml.designer.cs file have access to, still cannot edit because updates automatically , loses custom changes. in (linq2sql) property setter 1 of database tables used in application , appears problem caused call _dbaudiotrackcontributors.entity object:

public dbaudiotrackcontributor dbaudiotrackcontributors {         {         return this._dbaudiotrackcontributors.entity;     }     set     {         // last internal line before exception          dbaudiotrackcontributor previousvalue = this._dbaudiotrackcontributors.entity;          // execution never reaches here         if (((previousvalue != value) ||          (this._dbaudiotrackcontributors.hasloadedorassignedvalue == false)))         {             this.sendpropertychanging();             if ((previousvalue != null))             {                 this._dbaudiotrackcontributors.entity = null;                 previousvalue.dbaudiotrack = null;             }             this._dbaudiotrackcontributors.entity = value;             if ((value != null))             {                 value.dbaudiotrack = this;             }             this.sendpropertychanged("dbaudiotrackcontributors");         }     } } 

if has way provide me internal code entityref<tentity> struct (or entity property), possibly me work out causing problem.

the last line of own code before invalidoperationexception thrown call datacontext.submitchanges():

public int updateaudiotrack(audiotrack audiotrack) {     using (transactionscope transactionscope = new transactionscope())     {         using (midasdatacontext datacontext = datacontext)         {             dbaudiotrack dbaudiotrack = datacontext.dbaudiotracks.where(                 g => g.id == audiotrack.id).firstordefault();             if (dbaudiotrack == null) return -1;             copytodbaudiotrack(audiotrack, dbaudiotrack);             updateaudiotrackcontributors(datacontext, audiotrack);              // last line of code before exception              datacontext.submitchanges(conflictmode.failonfirstconflict);              // execution never reaches here             transactionscope.complete();             return 0;         }     } } 

the copytodbaudiotrack method copies of property values audiotrack object linq2sql generated dbaudiotrack object , updateaudiotrackcontributors method shown below.

private void updateaudiotrackcontributors(midasdatacontext datacontext, audiotrack audiotrack) {     datalist<label> labels = new datalist<label>(         audiotrack.labels.except(audiotrack.originalstate.labels));     if (labels.count > 0) addaudiotrackcontributors(datacontext, audiotrack, labels);     labels = new datalist<label>(audiotrack.originalstate.labels.except(audiotrack.labels));     if (labels.count > 0) deleteaudiotrackcontributors(datacontext, audiotrack, labels); } 

this method finds label objects have changed , either adds or deletes them in audiotrackcontributors database table. table seems root of problem disappears if code commented out. however, method correctly selects objects add or delete, still confused. addaudiotrackcontributors method calls code shown below , deleteaudiotrackcontributors code shown below that:

list<dbaudiotrackcontributor> dbaudiotrackcontributors = new list<dbaudiotrackcontributor>(); foreach (t datalistentry in datalist) {     dbaudiotrackcontributor dbaudiotrackcontributor = new dbaudiotrackcontributor();     copytodbaudiotrackcontributor(audiotrack, datalistentry, dbaudiotrackcontributor, contributortype);     dbaudiotrackcontributors.add(dbaudiotrackcontributor); } datacontext.dbaudiotrackcontributors.insertallonsubmit(dbaudiotrackcontributors); 

deleteaudiotrackcontributors:

list<dbaudiotrackcontributor> dbaudiotrackcontributors = new list<dbaudiotrackcontributor>(); foreach (t datalistentry in datalist) {     dbaudiotrackcontributor dbaudiotrackcontributor = datacontext.dbaudiotrackcontributors.where(d => d.datalistid == datalistentry.id && d.audiotrackid == audiotrack.id).firstordefault();     if (dbaudiotrackcontributor != null) dbaudiotrackcontributors.add(dbaudiotrackcontributor); } datacontext.dbaudiotrackcontributors.deleteallonsubmit(dbaudiotrackcontributors); 

again, stepping through code during execution shows above code correctly selects right objects delete. can't see problems in code, don't know next.

if has ideas on how proceed, glad hear them. many in advance.

ok, hideous problem no clear clues actual cause. turned out definition of dbaudiotrackcontributor table in linq2sql dbml file.

luckily, had dbmastertrackcontributor table did not suffer same error. after comparing of code related 2 tables , update, find no differences. then, decided @ linq2sql generated designer code , noticed difference between 2 table definitions.

the dbaudiotrackcontributor table registered entityref<dbaudiotrackcontributor> whereas working dbmastertrackcontributor table registered entityset<dbmastertrackcontributor>.

after looking online @ differences between them, turned out entityref<t> used 1 one relationships , entityset<t> used 1 many , many many relationships. therefore suspected should both have been declared entityset<t>, deleted table dbml file , re-added it.

after doing this, began work again! learnt valuable lesson: if have dbml related error, first try removing , re-adding suspect table(s) dbml file.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -