开发者

JsonNetResult not returning S#arp Nhibernate object

开发者 https://www.devze.com 2023-01-10 05:42 出处:网络
I am having a problem returning a JsonNetResult for an object when it is a property of another object, however when I fetch the object explicitly it works e.g.

I am having a problem returning a JsonNetResult for an object when it is a property of another object, however when I fetch the object explicitly it works e.g.

JsonNetResult res = new JsonNetResult();  
 res.Formatting = Formatting.Inde开发者_Python百科nted;  
 res.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;  
 res.Data = addressRepository.Get(7);  
 return res;

returns a valid result however

JsonNetResult res = new JsonNetResult();  
res.Formatting = Formatting.Indented;  
res.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;  
res.Data = businessRepository.Get(businessID).Address;  
return res;

will return an empty object; as will

JsonNetResult res = new JsonNetResult();  
res.Formatting = Formatting.Indented;  
res.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
res.Data =  addressRepository.Get(businessRepository.Get(businessID).Address.Id);  
return res;`

even though the address and it's ID is exactly the same in each situation. Is there something really obvious I'm missing?


This sounds like a lazy-load problem, which happens when trying to hydrate Json objects. Is the address property a string or does it reference another object? Make sure there's no recursive references, though JsonNetResult should deal with that.

Go into your entity's mapping and add this:

mapping.References(x => x.Address).Not.LazyLoad();

That should take out any lazy load proxies out of the equation.

0

精彩评论

暂无评论...
验证码 换一张
取 消