使用RIA服务域服务的EF4中包含()不加载! [英] Include() in EF4 using RIA Services Domain Service not loading!

查看:139
本文介绍了使用RIA服务域服务的EF4中包含()不加载!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Include()方法,我无法返回多个实体(渴望​​加载)。我正在尝试使用RIA服务将Silverlight应用程序加载到Silverlight应用程序中。数据模型由以下关系组成:



Events.ID = EventParticipants.EventsID

EventParticipants.ParticipantUserID = Users.ID



所以一个事件可以有很多参与者,一个参与者正是1个用户。



我可以加载事件,但是我无法从Include()语句指定的子关系中获取任何



当我加载一个事件时,我试图拉in(使用热切加载)所有EventParticipants及其关联的用户记录。在我的域服务中,我有以下内容:

  public IQueryable< Event> GetEvents()
{
return this.ObjectContext.Events
.Include(EventParticipants)
.Include(EventParticipants.User);

}

然后我将返回的IQueryable加载到我的域上下文如下:

  DomainContext.Load(DomainContext.GetEventsQuery(),LoadOperationCompleted,true); 

(通常我会过滤这个,但我简化了一切,以达到问题的核心) p>

在我的LoadOperationCompleted中,我有2个循环,用于查看是否返回事件参与者,但我从来没有收到任何参与者返回查询。 (在数据库中,我定义了242个事件...所有这些都有参与者)

  private void LoadOperationCompleted(LoadOperation lo) 
{

if(lo.Error!= null)
{
Debugger.Break();
lo.MarkErrorAsHandled();
}

foreach(在lo.Entities中的事件项目)
{

foreach(item.EventParticipants中的var particpant)
{
Debug.WriteLine({0} {1},particpant.User.First,particpant.User.Last);
}

}

}

我还修改了上面的代码(见下文)来旋转原始域上下文,而不是通过加载操作参数传递的实体集合无效。 (事件在那里,孩子们不是)

  foreach(DomainContext.Events中的事件项)

在我的数据模型中,有三个不同参数的构造函数(全部生成)。在每个我都禁用懒惰加载如下:

  this.ContextOptions.LazyLoadingEnabled = false; 

DataModel.edmx的xml表示中有一个注释。我认为这只是为了生成目的...但是我也改变了:

 < EntityContainer Name =MyCrazyEntities 注释:LazyLoadingEnabled =false> 

我已经在SQL Server 2008数据库中定义了关系。设计人员挑选了这些,并在我的模型中创建了所有的关系和导航属性。检查它们,它们似乎是有效的。如果我用手指在include语句中实体的拼写,它会抛出一个错误,指出该路径无效(或者某种事情)),所以我相信这些关系存在并且是有用的(我可以看到他们的定义在设计器代码和whatnot)。



我已经删除了所有的过滤,特别是在发布之前已经对查询放置的附加联接。这是为了确保我没有遇到连接改变查询形状并打破Includes()的问题。似乎是一个很大的问题,我以这种方式消除了所有的联接,以隔离问题。它不是联合的组合,包括。



我已经重建了我的Silverlight项目,这些项目包含域服务和RIA服务DLL几次,认为我有一些设置错误。我已经使用POCO类生成器与EF,但是然后读取您不能包含与POCO相同的方式...所以我也废弃了,并返回到默认实体生成器。



我启动了SQL Server分析器,并从基本查询中捕获了SQL,并且使用了Includes()。包含 ARE BEING RETURNED 的数据。我通过从profiler复制查询并直接发出来验证了这一点。查询的简化版本如下。关系是有效的,我可以看到所有相关的数据都存在(即参与者姓名,电话号码等)。

  SELECT $ 

FROM(SELECT
'trimmed fields for shortvity>'
CASE WHEN([Join1]。[ID1] IS NULL)THEN CAST(NULL AS int [EL] 1 END AS [C1]
FROM [dbo]。[Events] AS [Extent1]
LEFT OUTER JOIN(SELECT'trimms fields for shortvity>'
FROM [dbo] [EventParticipants] AS [Extent2]
INNER JOIN [dbo]。[Users] AS [Extent3] ON [Extent2]。[ParticipantUsersID] = [Extent3]。[ID])AS [Join1] ON [Extent1]。 [ID] = [Join1]。[EventsID]
)AS [Project1]
ORDER BY [Project1]。[ID] ASC,[Project1]。[C1] ASC

但是,当返回实体时,Event.EventParticipants EntityCollection为空。可悲的是。令人沮丧。痛苦。 (我已经离开了)



我已经在互联网上找到了解决方案,但是还没有找到任何与之相同问题的人解决了lazyloading设置,联合的组合和包括或不适当的导航属性。



我刚刚错过了一些基本的东西?有没有一个我错过的主要概念?我有一个来自老雇主的另一个项目,他们执行相同的操作,它似乎工作。所以我确信可以做到这一点。只是不是我吗?



非常感谢任何帮助。我在我的智慧结束。 提前感谢

解决方案

您是否尝试使用composition / include属性?
这告诉ria服务序列化entitycollection,还有几个链接,让你在正确的路径。



通过WCF RIA服务属性的指南



WCF RIA服务第5部分 - 元数据和共享类



RIA服务中的构成支持


I am having trouble returning multiple entities (eager loading) using the Include() method. I am trying to load the entites in a silverlight application using RIA services. The Data Model consists of the following relationships:

Events.ID = EventParticipants.EventsID
EventParticipants.ParticipantUserID = Users.ID

So an Event can have many participants and a participant is exactly 1 user.

I can load the Event just fine, but I cannot get anything out of the child relationships specified by the Include() statement.

When I load up an Event, I am trying to pull in (using eager loading) all the EventParticipants and their associated user record. In my domain service I have the following:

    public IQueryable<Event> GetEvents()
    {
        return this.ObjectContext.Events                
                    .Include("EventParticipants")
                    .Include("EventParticipants.User");

    }

I then take the IQueryable that is returned and load it using my Domain Context on the client side like so:

DomainContext.Load(DomainContext.GetEventsQuery(), LoadOperationCompleted, true);

(normally I would filter this but I simplified everything to get to the heart of the problem)

In my LoadOperationCompleted I have 2 loops that I use to see if the Event Participants are returned but I never receive any participants back from the query. (in the database, I have 242 Events defined...all of which have participants)

    private void LoadOperationCompleted(LoadOperation lo)
    {

        if (lo.Error != null)
        {
            Debugger.Break();
            lo.MarkErrorAsHandled();
        }

        foreach (Event item in lo.Entities)
        {

            foreach (var particpant in item.EventParticipants)
            {
                Debug.WriteLine("{0} {1}", particpant.User.First, particpant.User.Last);
            }

        }

    }

I have also modified the above code (see below) to spin through the original domain context instead of the entities collection that is passed in through the load operation parameters to no avail. (events are there, children are not)

foreach (Event item in DomainContext.Events)

In my Data Model, there are three constructors (all generated) with varying parameters. In each I have disabled lazy loading like so:

this.ContextOptions.LazyLoadingEnabled = false;

There was an annotation in the xml representation of the DataModel.edmx. I think this is just for generation purposes...but I changed that too like so:

<EntityContainer Name="MyCrazyEntities" annotation:LazyLoadingEnabled="false">

I have defined relationships in my SQL Server 2008 database. The designer picked these up and created all of the relationships and navigation properties in my model. Inspecting them, they appear valid. If I fat finger the spelling of the Entities in the include statement, it throws an error saying the path is not valid (or something to that effect)...so I believe the relationships exist and are functional (and I can see their definitions in the designer code and whatnot).

I have removed all the filtering and specifically the additional joins I have placed on the query before issuing it. This was to ensure I wasn't encountering the problem with joins changing the shape of the query and breaking the Includes(). Seems to be a big problem for people, I have eliminated all joins in this manner to isolate the problem. It is not a combination of joins and includes.

I have rebuilt my silverlight projects that house the domain service and RIA Services DLL a couple of times thinking I had some setting wrong. I have used the POCO class generator with EF but then read you can't include the same way with POCOs...so I scrapped that too and went back to the default entity generator.

I fired up SQL Server profiler and captured the SQL from a base query and one with Includes(). The data for the Includes ARE BEING RETURNED. I verified this by copying the query from profiler and issuing it directly. A simplified version of the query is as follows. The relationships are valid and I can see all the related data is there (i.e. participant names, phone numbers, etc)

SELECT 
'trimmed fields for brevity>'
FROM ( SELECT 
    'trimmed fields for brevity>'
    CASE WHEN ([Join1].[ID1] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM  [dbo].[Events] AS [Extent1]
        LEFT OUTER JOIN  (SELECT 'trimmed fields for brevity>'
        FROM  [dbo].[EventParticipants] AS [Extent2]
        INNER JOIN [dbo].[Users] AS [Extent3] ON [Extent2].[ParticipantUsersID] = [Extent3].[ID] ) AS [Join1] ON [Extent1].[ID] = [Join1].[EventsID]
)  AS [Project1]
ORDER BY [Project1].[ID] ASC, [Project1].[C1] ASC

However, when the entity is returned, the Event.EventParticipants EntityCollection is empty. Sadly. Frustratingly. Painfully. (I am out of "ly"s)

I have scoured the internet for solutions but haven't found anyone with the same problem that wasn't solved by the lazyloading setting, the combination of joins and includes or inappropriate navigation properties.

Have I just missed some basic thing? Is there a major concept that I am missing? I have another project from an old employer where they perform this same operation and it appears to work. So I am sure it can be done. Just not by me??

Any help is greatly appreciated. I am at my wits end. Thanks in advance!

解决方案

Did you try attribute the collection on your model with the composition/include attribute? this tells ria services to serialize the entitycollection as well, heres a couple of links to get you on the right path

A guide through WCF RIA Services attributes

WCF RIA Services Part 5 - Metadata and Shared Classes

Composition Support in RIA Services

这篇关于使用RIA服务域服务的EF4中包含()不加载!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆