简体   繁体   English

Include 内部使用的 Lambda 表达式无效。 EF6,导航属性

[英]Lambda expression used inside Include is not valid. EF6, Navigation Property

I was trying to solve a many to many relation.我试图解决多对多的关系。 On a database level everthing looks good to me, but when I tryr to get that data( inlcude ), it fails.在数据库级别上,一切对我来说都很好,但是当我尝试获取该数据( inlcude )时,它失败了。

I get:我得到:

Lambda expression used inside Include is not valid. Include 内部使用的 Lambda 表达式无效。

What I call/tried:我叫/试过的:

var readRecipes = db.MyClasses. 
                     Include(blog => blog.SomeCollections).ThenInclude(post => post.Prop1).
                     Include(blog => blog.SomeCollections).ThenInclude(post => post.Prop2).

Structure结构

MainClass:主类:

public class MainClass{
// ...                   
public ICollection<NavigationProperty> NavigationPropertys; 
// ...               
public MainClass()
{

    this.NavigationPropertys = new Collection<NavigationProperty>(); 
}
}

NavigationProperty:导航属性:

public class NavigationProperty
    {
        public Guid ID { get; set; }



        [Required]
        public Guid? Prop1ID { get; set; }// 0,*

        [Required]
        public Guid? Prop2ID { get; set; }// 0,*

        [Required]
        public Guid MainClassID { get; set; }



        [Required]
        public Prop1 Prop1 { get; set; }

        [Required]
        public Prop2 Prop2 { get; set; }




        public MainClass MainClass { get; set; }



        [Required]
        public float Amount { get; set; }


        public NavigationProperty()
        {
            // todo: ?
        }
    }

I don't know, what I did wrong?我不知道,我做错了什么?

Ok, I solved my question.好的,我解决了我的问题。

Instead of:代替:

public ICollection<NavigationProperty> NavigationPropertys; 

I should have used:我应该使用:

public ICollection<NavigationProperty> NavigationPropertys { get; set; }; 

This made Lambda crash.这使得 Lambda 崩溃。

Thank You.谢谢你。

If you want to include the children of a navigational property as well you should do:如果您还想包含导航属性的子项,您应该这样做:

Parent.Include(p => p.Children.Select(c => c.GrandChild));

or if there are many grand children per child或者如果每个孩子有很多孙子

Parent.Include(p => p.Children.SelectMany(c => c.GrandChildren));

or if there is only one child and one grand child per parent.或者如果每个父母只有一个孩子和一个孙子。

Parent.Include(p => p.Child.GrandChild);

I hope this helps.我希望这有帮助。

EDIT: I didn't realise that your question was regarding ef-core.编辑:我没有意识到你的问题是关于 ef-core。 I thought it was some type of pseudo code or self made extension method.我认为这是某种伪代码或自制扩展方法。

Have you read through all the examples provided by Microsoft regarding this topic?您是否已阅读 Microsoft 提供的有关此主题的 所有示例 Otherwise I'm afraid that I don't have enough info to assist you in the matter.否则,恐怕我没有足够的信息来帮助你处理这件事。

One thing that I'll need to see is how you configured the relationship between Prop1 and the Navigation entity.我需要了解的一件事是您如何配置Prop1和 Navigation 实体之间的关系。 I don't see a foreign key in your model.我在您的 model 中没有看到外键。 Of course this is not necessary depending on how you configured the relationship in the overridden OnModelCreating method using the modelBuilder in your DbContext or how you used the attributes to indicate the relationship.当然,这不是必需的,具体取决于您如何使用modelBuilder中的DbContext在重写的OnModelCreating方法中配置关系,或者您如何使用属性来指示关系。 More about relationships and specifying relationships in ef-core can be foundhere .有关 ef-core 中的关系和指定关系的更多信息,请参见此处

暂无
暂无

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

相关问题 EF Core 3.1 - 包含过滤抛出错误“包含内部使用的 Lambda 表达式无效。” - EF Core 3.1 - Include filtering throw an error 'Lambda expression used inside Include is not valid.' Include 中使用的 Lambda 表达式无效。 包括不工作 - Lambda expression used inside Include is not valid. Include not working EF Core 过滤包含:“包含中使用的 Lambda 表达式无效” - EF Core Filtered Include: “Lambda expression used inside Include is not valid” EF Core:在 Include 中使用的 Lambda 表达式无效 - EF Core: Lambda expression used inside Include is not valid 在Include中使用的Lambda表达式无效 - Lambda expression used inside Include is not valid EF 核心 ThenInclude Select 说“Include 中使用的 Lambda 表达式无效” - EF core ThenInclude Select says "Lambda expression used inside Include is not valid" Entity Framework Core 3.0 - Include 中使用的 Lambda 表达式无效 - Entity Framework Core 3.0 - Lambda expression used inside Include is not valid 在 Include 中使用的 Lambda 表达式在 Entity Framework Core 中无效 - Lambda expression used inside Include is not valid in Entity Framework Core EF Core lambda expr 内包含无效 - EF Core lambda expr inside include not valid 指定的包含路径无效。 EntityType&#39;SpiceShop.Models.Product&#39;未声明名为&#39;Products&#39;的导航属性 - A specified Include path is not valid. The EntityType 'SpiceShop.Models.Product' does not declare a navigation property with the name 'Products'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM