当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - SingleOrDefault inside Include will raise the following error :- The Include path expression must refer to a navigation property defined on the type

问题描述:

I am working on an asp.net mvc 4 web application, and I am using Entity Framework 5. I have the following model method:

public SDJoin AllFindSDDetails_J(int id)

{

var IT360Resource = entities.Resources

.Include(a => a.ComponentDefinition)

.Include(a => a.ResourceLocation.SiteDefinition.SDOrganization)

.Include(a => a.ResourceLocation.SiteDefinition.AccountDefinitions1.SingleOrDefault().SDOrganization)

.Include(a => a.ResourceState)

.SingleOrDefault(a2 => a2.RESOURCEID == tmsSD.Technology.IT360ID);

but when this method is being called I get the following exception:

The Include path expression must refer to a navigation property

defined on the type. Use dotted paths for reference navigation

properties and the Select operator for collection navigation

properties. Parameter name: path Description: An unhandled

exception occurred during the execution of the current web request.

Please review the stack trace for more information about the error and

where it originated in the code.

Exception Details: System.ArgumentException: The Include path

expression must refer to a navigation property defined on the type.

Use dotted paths for reference navigation properties and the Select

operator for collection navigation properties.

I fixed it by replacing this :

 .Include(a => a.ResourceLocation.SiteDefinition.AccountDefinitions1.SingleOrDefault().SDOrganization)

with :

 .Include(a => a.ResourceLocation.SiteDefinition.AccountDefinitions1.Select(aa=>aa.SDOrganization))

So I replaced SingleOrDefault() by Select(aa=>aa...)

My question is: why can I not define SingleOrDefault() inside .Include()

Second question: why do I not get any compilation error? I mean I successfully built my project, but at run-time I got the exception when the method was called.

Thanks

网友答案:

Did you set any mapping? I don't see any mapping here. You may need to specifically set you object relationships using mapping via either fluent or attribute based mapping entity mapping

分享给朋友:
您可能感兴趣的文章:
随机阅读: