Featured post
asp.net mvc - MVC Entity Framework Model not returning correct data -
run strange problem while writing asp.net mvc site. have view in sql server database returns few date ranges. view works fine when running query in ssms.
when view data returned entity framework model, returns correct number of rows of rows duplicated.
here example of have done:
sql server code:
edited: (table a)
create table [dbo].[a]( [id] [int] not null, [phid] [int] null, [fromdate] [datetime] not null, [todate] [datetime] null, constraint [pk_a] primary key clustered ( [id] asc, [fromdate] asc )) on [primary] create table [dbo].[b]( [phid] [int] not null, [fromdate] [datetime] null, [todate] [datetime] null, constraint [pk_b] primary key clustered ( [phid] asc )) on [primary] go create view c select a.id, case when a.phid null a.fromdate else b.fromdate end fromdate, case when a.phid null a.todate else b.todate end todate left outer join b on a.phid = b.phid go insert b (phid, fromdate, todate) values (100, '20100615', '20100715') insert (id, phid, fromdate, todate) values (1, null, '20100101', '20100201') insert (id, phid, fromdate, todate) values (1, 100, '20100615', '20100715') insert b (phid, fromdate, todate) values (101, '20101201', '20101231') insert (id, phid, fromdate, todate) values (2, null, '20100801', '20100901') insert (id, phid, fromdate, todate) values (2, 101, '20101201', '20101231')
so now, if select c, 4 separate date ranges
in entity framework model (which call 'core'), view 'c' added.
in mvc controller:
public class homecontroller : controller { public actionresult index() { coreentities db = new coreentities(); var clist = c in db.c select c; return view(clist.tolist()); } }
in mvc view:
@model list<rm.models.c> @{ foreach (rm.models.c c in model) { @string.format("{0:dd-mmm-yyyy}", c.fromdate) <span>-</span> @string.format("{0:dd-mmm-yyyy}", c.todate) <br /> } }
when run this, outputs this:
01-jan-2010 - 01-feb-2010 01-jan-2010 - 01-feb-2010 01-aug-2010 - 01-sep-2010 01-aug-2010 - 01-sep-2010
when should (this view returns):
01-jan-2010 - 01-feb-2010 15-jun-2010 - 15-jul-2010 01-aug-2010 - 01-sep-2010 01-dec-2010 - 31-dec-2010
also, i've run sql profiler on , according that, query being executed is:
select [extent1].[id] [id], [extent1].[fromdate] [fromdate], [extent1].[todate] [todate] (select [c].[id] [id], [c].[fromdate] [fromdate], [c].[todate] [todate] [dbo].[c] [c]) [extent1]
which returns correct data
so seems entity framework doing data in meantime.
to me, looks fine! have missed something?
cheers, ben
edit:
sorry, table should be:
create table [dbo].[a]( [id] [int] not null, [phid] [int] null, [fromdate] [datetime] not null, [todate] [datetime] null, constraint [pk_a] primary key clustered ( [id] asc, [fromdate] asc )) on [primary]
i figured out myself.
the problem way view mapped in entity model.
when added, made entity key id. needed on id , fromdate. included fromdate in entity key , works fine.
- Get link
- X
- Other Apps
Comments
Post a Comment