1

我正在研究我的第一个 EntityFramework 解决方案,但我很困惑。

当我遍历生成的“日历”实体对象时,该属性calendar.ref_calendar_premisis.premisis未实例化(null)。我明白为什么,我停用了延迟加载,因为我想控制语法树投影中的条件“包含”,如下所示:

    private List<CalendarBlockDTO> FillUserCalendar(DateTime start, DateTime end, int uid, bool everything)
    {
        var result = new List<CalendarBlockDTO>();

        using (var db = new ViggoEntities())
        {
            //We need to disable lazyloading to use the "expression tree" syntax
            db.Configuration.LazyLoadingEnabled = false;

            //flip our "everything" so we can compare it to our "special" column
            int specialScope = Convert.ToInt32(!everything);

            //Build a query "Projection" with "expression tree" syntax
            var query = from c in db.calendars
                        select new
                            {
                                calendarEntry = c,

                                createdByUser = c.MadeByUser,

                                premisesBookings = c.ref_calendar_premises.Where
                                (
                                    rcp => rcp.deleted == 0 &&
                                     (
                                            //started before the start-parameter AND ended after start-parameter
                                        (rcp.timestart < start && rcp.timeend > start) ||
                                            //OR startet before the end-parameter AND ended after the end-parameter
                                        (rcp.timestart < end && rcp.timeend > end) ||
                                            //OR startet before the start-parameter AND ended after the end-paremeter
                                        (rcp.timestart < start && rcp.timeend > end) ||
                                            //OR startet after the start-parameter AND ended before the end-parameter
                                        (rcp.timestart > start && rcp.timeend < end)
                                    )
                                ),

                                attendingGroups = c.ref_groups_calendar.Where
                                (
                                    rug => rug.deleted == 0
                                ),

                                groups = c.ref_groups_calendar.Select( rgc => rgc.usergroup ),

                                ////Assignments not implemented yet
                                ////assignments = c.

                                schedules = c.ref_calendar_schedule.Where
                                (
                                    sch => sch.deleted == 0
                                )
                            };

            var calEntries =
                query.ToArray().Select(c => c.calendarEntry).
                Where(
                        //If only special requested, show only special
                        c => c.special >= specialScope &&
                        //If not "MadeInInfo", show for creator as well
                        (c.madeininfo==0 && c.made_by == uid) || 
                        //Else, show to involved users
                        (c.ref_groups_calendar.Any(rgc => rgc.usergroup.ref_users_groups.Any(rug => rug.userid == uid)))
                        );

            foreach (var calendar in calEntries)
            {
                //I WANT THIS TO NOT THROW AN EXCEPTION, PREMIS SHOULD NOT BE NULL
                if (calendar.name == "Dinner with Allan" && calendar.ref_calendar_premises.Any(rcp => rcp.premis == null))
                    throw new Exception("Premis not instantiated!");

                result.AddRange(CalendarToCalendarBlockDTOs(calendar));
            }
        }

        return result;
    }

我尝试添加类似:

...
room = c.ref_calendar_premises.Select(r => r.premis),
...

......但无济于事。在我们的测试数据中,已经为“与艾伦共进晚餐”活动预订了一个房间,但我似乎无法让它加载 premis-entyties。

我以前没有使用 EntityFramework、LINQ to SQL 或任何其他 ORM 的经验,所以我可能遗漏了一些显而易见的东西。

有什么建议么?

4

1 回答 1

0

我扭动了我们负责数据库的手臂,结果发现所有行的 premisisid 都是空的,因为转换错误(我正在查看 testdata 文档,而不是实际数据)。所以感谢您的投入和您的时间,我会展示自己 0_0

于 2012-12-17T10:45:57.450 回答