EF4: NavigationProperty and join: is a bug or a feature?

前提是你 提交于 2020-01-07 07:47:08

问题


I’d like to consult about a problem I have faced. I've started working on a project with a very difficult database: many tables in the DB don’t have primary keys or have multiple PKs, so I can't add correct associations for all entities in my edmx. However, for some entities it’s possible and I managed to do so. Thus, I have two entities with an association between them: Vulner and VulnerDescription. And I have a "bad" connection table for Vulners called VulnerObjectTie (with a mental FK: VulnerObjectTie.Vulner = Vulner.Id), which I can’t add correct associations to. So, I decided to do add the following LinqtoEntities query:

            var vulerIdList = from vulner in _businessModel.DataModel.VulverSet.Include("Descriptions")
                    join objectVulnerTie in _businessModel.DataModel.ObjectVulnerTieSet on vulner.Id equals objectVulnerTie.Vulner
                    where softwareId.Contains(objectVulnerTie.SecurityObject)
                    select vulner;

where description is Navigation Property for an association with the VulnerDescription table. The query works, but it does not load the Descriptions property. However, if I remove the join operator, then Descriptions are loaded correctly.

The most obvious solution for this problem is to divide one query into the next two queries:

            var vulerIdList = from vulner in _businessModel.DataModel.VulverSet
                    join objectVulnerTie in _businessModel.DataModel.ObjectVulnerTieSet on vulner.Id equals objectVulnerTie.Vulner
                    where softwareId.Contains(objectVulnerTie.SecurityObject)
                    select vulner.Id;

        var query = from vulner in _businessModel.DataModel.VulverSet.Include("Descriptions")
                    where vulerIdList.Contains(vulner.Id)
                    select vulner;

But I think it looks ugly. Can anyone suggest a more simple solution for this problem, or is it just a special feature of EF4??

thankyouplease :))


回答1:


It's a known 'feature' or limitation, depending on how you look at it. Here's an interesting discussion on the topic, I'm sure there are more references to find: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/d700becd-fb4e-40cd-a334-9b129344edc9/




回答2:


The problem here is that EF is not very well suited for "bad databases". EF (and especially all automation tools like model wizard) expects clear and correct database design.

Include is not supported in queries using custom joins or projections. Not supported in this case means that it is completely omitted.



来源:https://stackoverflow.com/questions/7886285/ef4-navigationproperty-and-join-is-a-bug-or-a-feature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!