NHibernate.Linq, WCF RIA Services, strange error

I have a Silverlight Business Application application project with these codes.

I have this domain class:

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; }
}

The mapping of domain objects has been configured and is working fine.

I have this DTO class:

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;
    }
}

DTO is used to smooth an object and ensure that no unnecessary relationships are serialized or wired.

Then I have this RIA DomainService (there are several options for the GetSectors () method, I will explain later):

[EnableClientAccess]
public class OrganizationService : BaseDomainService
{
    public IQueryable<SectorDto> GetSectors1()
    {
        return GetSession().Linq<Sector>()
               .Select(x => Mapper.Map<Sector, SectorDto>(x));
    }

    public IQueryable<SectorDto> GetSectors2()
    {
        return GetSession().Linq<Sector>().ToList()
               .Select(x => new SectorDto(x)).AsQueryable();
    }

    public IQueryable<SectorDto> GetSectors3()
    {
        return GetSession().Linq<Sector>().Select(x => new SectorDto(x));
    }

    public IQueryable<SectorDto> GetSectors4()
    {
        return GetSession().Linq<Sector>().Select(x => new SectorDto() { 
            Id = x.Id, CreatedOn = x.CreatedOn, Name = x.Name, Code = x.Code });
    }
}

BaseDomainService is the only parent class that provides NHibernate session processing. I set up a session for every web request.

Then I will connect the service to the DataGrid (Silverlight Toolkit) on the XAML page:

var ctx = new App.Web.Services.OrganizationContext();
SectorGrid.ItemsSource = ctx.SectorDtos;
ctx.Load(s.GetSectors1Query());

When calling various methods, I got the following results:

  • GetSectors1() " " GetSectors1 ". NHibernate.Linq.Expressions.EntityExpression ' ' NHibernate.Linq.Expressions.CollectionAccessExpression".".

    , . AutoMapper, DTO. , AutoMapper, , , Select, . GetSession().Linq<Sector>().Select(x => CustomMap(x)).

  • GetSectors2() , IQueryable, .

  • GetSectors3() , Id CreatedOn, BaseDomain. .

  • GetSectors4() , DTO , !

, ? , ! , ? ?

, . .

+3
3

GetSession(). Linq() .ToList(). (x = > CustomMap (x))

. http://www.mail-archive.com/nhusers@googlegroups.com/msg12003.html

0

, , , , - select:

:

public Expression<Func<Sector,SectorDto>> EntityToDto = 
        x => new SectorDto
            {
                Id = d.Id;
                CreatedOn = d.CreatedOn;
                Code = d.Code;
                Name = d.Name;
            };

:

public IQueryable<SectorDto> GetSectors4()
    {
        return GetSession().Linq<Sector>().Select(EntityToDto);
    }

Dto. , RIA, :). ( , , ), , . , . , !

0

, select .ToList() - , NHibernate SQL.

Nhibernate tries to convert your LINQ statement with your choice into SQL when .ToList () is called. It cannot convert your automapper code to SQL (which is in the selected status). This is why you need to first select an object from the database, and then call .ToList () to execute SQL, and then iterate over the list and display it.

0
source

Source: https://habr.com/ru/post/1734506/


All Articles