Friday, 15 January 2010

c# - Entity Framework 6- lazy loading expect empty collection but return null (only in test) -



c# - Entity Framework 6- lazy loading expect empty collection but return null (only in test) -

i utilize ef6 code first model lazy loading. have 1 normal project (asp.net mvc) , 1 tests it. create in init (drop exsists before) normal database testing (no mocking). utilize same initializer in both project (and find problem seek utilize same connectionstring creating context).

when utilize property witch collection (data other table) no elements, in normal project ef send sql request (i utilize sql server profiler check it), in testing project property null. if collection isn't empty works right in both cases.

this simple version of code (please tell if need sth more, no have thought mistake): info models:

public class story { public int id { get; set; } public virtual icollection<comment> comments { get; set; } } public class comment { public int id { get; set; } [index("commentclusteredindex_storyid", isclustered = true)] public int storyid { get; set; } public virtual story story { get; set; } }

web.config(project), app.config(tests):

<configsections> <section name="entityframework" type="system.data.entity.internal.configfile.entityframeworksection, entityframework, version=6.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" requirepermission="false" /> </configsections> <connectionstrings> <add name="storiesentitiestestcontext" connectionstring="data source=(localdb)\v11.0;initial catalog=storiesentitiestestdb;integrated security=true;" providername="system.data.sqlclient" /> </connectionstrings> <entityframework> <defaultconnectionfactory type="system.data.entity.infrastructure.localdbconnectionfactory, entityframework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultconnectionfactory> <providers> <provider invariantname="system.data.sqlclient" type="system.data.entity.sqlserver.sqlproviderservices, entityframework.sqlserver" /> </providers> </entityframework>

it's how used - code exsist in normal project (this 1 without tests):

public void method(dbcontext context) { dbset<story> _dbset = context.set<story>(); var = dbset.first().comment; //when comment doesn't have items null in testing project , empty collection in normal project }

if wanna empty collections instead of nulls, have add together constructor entity collection , in constructor initialize empty collection.

public class story { public int id { get; set; } public virtual icollection<comment> comments { get; set; } public story() { comments = new list<comment>(); } }

something that.

c# asp.net asp.net-mvc entity-framework lazy-loading

No comments:

Post a Comment