Monday 15 July 2013

asp.net mvc 5 - how to desactivate lazy loading for entity framework 6 -



asp.net mvc 5 - how to desactivate lazy loading for entity framework 6 -

public class country : item { public string code {get; set;} public string codephone { get; set; } public string name {get; set;} public string flag { get; set; } public decimal? latitude {get; set;} public decimal? longitude {get; set;} public int regionscount {get; set;} [foreignkey("defaultcurrency")] public int? defaultcurrencyid {get; set;} public virtual currency defaultcurrency { get; set; } public continenttype continenttype { get; set; } public virtual icollection<property> properties {get; set;} public icollection<countrylocale> countrylocales { get; set; } } public class countrylocale : itemlocale { [foreignkey("country")] public int countryid {get; set;} public country country {get; set;} public string fullname {get;set;} } public tentity get(expression<func<tentity, bool>> where, params string[] includes) { var model = this.dbset; foreach (var property in includes) { model.asexpandable().include(property); } homecoming model.where(where).firstordefault(); } this.configuration.lazyloadingenabled = false; this.configuration.proxycreationenabled = false; country country = this._countryrepository.get(p=>p.id == this.countryid, new string[] { "countrylocales" });

value cannot null. parameter name: source

description: unhandled exception occurred during execution of current web request. please review stack trace more info error , originated in code.

exception details: system.argumentnullexception: value cannot null. parameter name: source line : countrylocale countrylo = country.countrylocales.first();

i got error when trying desactivate lazyloading performance reasons. how can resolve problem? in advance.

as disabled lazyloading need manually load kid properties of main object include

var country = db.country.include("countrylocales");

the other way around be:

dont disable lazy load , utilize include in queries.

try including kid objects 1 1 until sense performance better.

usually if know iterate on kid object should utilize include in query kid object.

remeber include sec level kid if need access property of level... include("parent.child")

asp.net-mvc-5 lazy-loading entity-framework-6

No comments:

Post a Comment