WCF returning wrong view output -


here struck in strange situation.

i created view in sql server

create view [adm].[vwsurveyreport]  select q.questionid,q.question,p.dtstart,p.dtend,u.orgcode,u.userid,u.roleid, (case tc.blanswer  when 1 1  when 0 0 end) ansyes, (case tc.blanswer  when 1 0  when 0 1 end) ansno dbo.surveyquestions q left outer join dbo.surveytranschild tc on tc.questionid=q.questionid left outer join dbo.surveytransparent tp on tp.surveytransid=tc.surveytransid left outer join dbo.surveyparent p on p.surveyid=tp.surveyid left outer join adm.masfeuser u on tp.userid=u.userid p.dtstart not null go 

and code behind

list<thepaywcfref.vwsurveyreport> data = new list<thepaywcfref.vwsurveyreport>(); data =  (from v in entities.vwsurveyreports      1 == 1       select v).tolist(); 

when run select query in sql server showing exact output want

userid  roleid  ansyes  ansno m6018upl    2   1   0 m6018upl    2   1   0 m6018app1   4   1   0 m6018app1   4   0   1 

but in code behind getting wrong data "data" list

userid  roleid  ansyes  ansno m6018app1   4   1   0 m6018app1   4   0   1 m6018app1   4   1   0 m6018app1   4   0   1 

ansyes, ansno , userid wrong

please me.

thanks in advance.

i don't know ef model looks - issue views in entity framework boils down this:

  • since view default doesn't have primary key, ef "infer" "entity key" based on columns in view guaranteed not null

  • those columns might (or might not) make surrogate primary key

  • when ef loads data, @ columns in primary key; if entity same entitykey has been loaded database, ef not load data again, reference entity has in memory

all can lead unexpected results on ef using views.

there number of solutions around this:

  • make sure include all primary keys included base tables in view; way, table's primary keys will part of view's surrogate primary key ef , guarantee uniqueness

  • create own, individual pseudo primary key having e.g. row_number() column in view, make sure each row has that's unique row, in order prevent ef "short-circuiting" entities because thinks it's loaded entity...


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 -