简体   繁体   English

我如何获得此条件包含投影以实例化ref_calendar_premis实体对象的“ premis”实体对象属性?

[英]How do i get this conditional include projection to instantiate the “premis” entity-object property of the ref_calendar_premis entity-object?

I'm working on my first EntityFramework-solution, and I'm stumped. 我正在研究我的第一个EntityFramework解决方案,但我很困惑。

When I'm iterating through the resulting "calendar"-entity objects, the property calendar.ref_calendar_premisis.premisis is un-instantiated ( null ). 当我遍历生成的“ calendar”实体对象时,未实例化calendar.ref_calendar_premisis.premisis属性( null )。 I understand why, I deactivated lazy-loading because i wanted to control the conditional "includes" in a syntax-tree projection like so: 我了解为什么,我停用了延迟加载,因为我想像这样在语法树投影中控制条件“包含”:

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

I tried adding something like: 我尝试添加类似的内容:

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

... But to no avail. ...但是无济于事。 A room has been booked for the "Dinner with Allan" event in our test data, but i cant seem to get it to load the premis-entyties. 在我们的测试数据中已经为“与Allan一起吃饭”事件预订了一个房间,但我似乎无法容纳这个房间。

I have no previous experience with EntityFramework, LINQ to SQL or any other ORM's, so I might be missing something plainly obvious. 我以前没有使用EntityFramework,LINQ to SQL或任何其他ORM的经验,因此我可能会缺少明显显而易见的内容。

Any suggestions? 有什么建议么?

I twisted the arm of our DB-responsible, and turns out premisisid is null for all rows because of a conversion error (i was looking at testdata documentation, not the actual data). 我扭曲了数据库负责人的手臂,结果由于转换错误,所有行的premisisid为null(我在看testdata文档,而不是实际数据)。 So thanks for the input and your time, I'll just show myself out 0_0 因此,感谢您的投入和宝贵的时间,我将向自己展示0_0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过Linq-to-Entity获得关系实体对象? - How to get a relational entity-object by Linq-to-Entity? 扩展实体对象以包含计算所得的属性 - Extend Entity Object to include a calculated property 实体框架的对象的Linq set属性无需SELECT进行额外投影 - Entity Framework Linq set property of an object without additional projection by SELECT 如何告诉实体框架不包含嵌套的 object? - How to tell Entity Framework to not include a nested object? 如何在投影实体框架对象中包含复杂实体字段? - How to include complex entity fields in a projected entity framework object? 如何从实体框架元数据EntityType对象中获取相应的POCO对象类型? - How do I get the corresponding POCO object Type from an Entity Framework Metadata EntityType object? 如何在实体框架中包含实体特定的属性? - How to include a entity specific property in Entity Framework? 实体对象的虚拟列表为空,我该如何初始化它? - Entity object's virtual list is null, how do I initialise it? 如何让 EF Core 使用存储库自动填充实体的子 object 中的值? - How do I get EF Core to automatically populate values in an entity's child object using a repository? 实体框架4:DeleteObject(entity):是否需要首先检索此对象? - Entity Framework 4: DeleteObject(entity): Do I need to retrieve this object first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM