c# - DbContext.Set<T>().SqlQuery Loading related entities -
i running below code:
var paramuserid = new sqlparameter { parametername = "userid", value = userid }; string query = string.format("{0} {1}", "usp_getitems", "@userid"); var results = _context.set<item>().sqlquery(query, paramuserid);
the usp_getitems
stored procedure have. navigation properties not being loaded. there anyway acomplish on entity framework?
because according question eager loading in entityframework dbcontext.database.sqlquery looks it's possible.
thanks
assuming stored proc returns denormalization of items
, users
, 1 idea comes mind use projection dto mimics structure of results of stored procedure, , use context.database.sqlquery<t>
flavour of sqlquery
project flattened dto.
you can use linq
again re-normalize result set entity graph, guess original intention.
in detail
assuming existing entities ef model:
public class item { public int itemid { get; set; } public string name { get; set; } } public class user { public int userid { get; set; } public string name { get; set; } public icollection<item> items { get; set; } }
create transfer dto represents flattened structure of stored proc result set:
public class useritemprocdto { public int itemid { get; set; } public string itemname { get; set; } public int userid { get; set; } public string username { get; set; } }
then project denormalized dto, , renormalize using linq:
var results = _context.sqlquery<useritemprocdto>(query, paramuserid); var userswithitems = results.groupby(r => r.userid) .select(g => new user { userid = g.key, name = g.first().username, items = g.select(i => new item { itemid = i.itemid, name = i.itemname }).tolist() });
this not want many stored procedures span multiple tables, of course :)
Comments
Post a Comment