Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate.Linq, WCF RIA Services, weird error
    primarykey
    data
    text
    <p>I have a Silverlight Business Application project set up, with these codes.</p> <p>I have this domain class:</p> <pre><code>public class BaseDomain { public virtual Guid Id { get; set; } public virtual DateTime CreatedOn { get; set; } } public class Sector : BaseDomain { public virtual string Code { get; set; } public virtual string Name { get; set; } } </code></pre> <p>The domain objects mapping has been set up and working fine.</p> <p>The I have this DTO class:</p> <pre><code>public class SectorDto : BaseDto { [Key] public virtual Guid Id { get; set; } public virtual DateTime CreatedOn { get; set; } public virtual string Code { get; set; } public virtual string Name { get; set; } public SectorDto() { } public SectorDto(Sector d) { Id = d.Id; CreatedOn = d.CreatedOn; Code = d.Code; Name = d.Name; } } </code></pre> <p>DTO is used to flatten the object and make sure no unnecessary relations from being serialized and transfered over the wire.</p> <p>Then I have this RIA DomainService (there are several variations of the GetSectors() method, I'll explain later):</p> <pre><code>[EnableClientAccess] public class OrganizationService : BaseDomainService { public IQueryable&lt;SectorDto&gt; GetSectors1() { return GetSession().Linq&lt;Sector&gt;() .Select(x =&gt; Mapper.Map&lt;Sector, SectorDto&gt;(x)); } public IQueryable&lt;SectorDto&gt; GetSectors2() { return GetSession().Linq&lt;Sector&gt;().ToList() .Select(x =&gt; new SectorDto(x)).AsQueryable(); } public IQueryable&lt;SectorDto&gt; GetSectors3() { return GetSession().Linq&lt;Sector&gt;().Select(x =&gt; new SectorDto(x)); } public IQueryable&lt;SectorDto&gt; GetSectors4() { return GetSession().Linq&lt;Sector&gt;().Select(x =&gt; new SectorDto() { Id = x.Id, CreatedOn = x.CreatedOn, Name = x.Name, Code = x.Code }); } } </code></pre> <p>BaseDomainService is just a parent class that provides handling for NHibernate session. I set the session to live per web request.</p> <p>Then I hook up the service to a DataGrid (Silverlight Toolkit) in a XAML page:</p> <pre><code>var ctx = new App.Web.Services.OrganizationContext(); SectorGrid.ItemsSource = ctx.SectorDtos; ctx.Load(s.GetSectors1Query()); </code></pre> <p>When calling the various methods, I got these results:</p> <ol> <li><p>Method <strong>GetSectors1()</strong> produce an exception <em>"Load operation failed for query 'GetSectors1'. Unable to cast object of type NHibernate.Linq.Expressions.EntityExpression' to type 'NHibernate.Linq.Expressions.CollectionAccessExpression'."</em>. </p> <p>This is the best way that I'm trying to achive. I want to use the <a href="http://automapper.codeplex.com/" rel="nofollow noreferrer">AutoMapper</a> library to automatically map the domain class to the DTO. I'm quite positive that the problem is not from the AutoMapper since I also get the error if I call a method from inside the anonymous method passed into Select, eg. <code>GetSession().Linq&lt;Sector&gt;().Select(x =&gt; CustomMap(x))</code>.</p></li> <li><p>Method <strong>GetSectors2()</strong> displays the data correctly in the grid, but this defeats the purpose of using IQueryable, the call would not be lazy evaluated.</p></li> <li><p>Method <strong>GetSectors3()</strong> fetches the data, but only the Id and CreatedOn which is in the parent class BaseDomain. The Code and Name are both null.</p></li> <li><p>Method <strong>GetSectors4()</strong> fetches the data correctly and lazily evaluated, but I don't want to manually map my domain to DTO every time like this!</p></li> </ol> <p>So what gives? The results are so far away than what I expected! Any idea on how to make this work? Any advice? </p> <p>I appreciate any help, I'm nearly lost. Thank you very much.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload