Entity framework Include command - Left or inner join?

前端 未结 2 1824
误落风尘
误落风尘 2020-12-30 20:56

As I was investigating the difference between Include and Join I found that :

If the DB does not include a Foreign Keys -it has n

相关标签:
2条回答
  • 2020-12-30 21:44

    Suppose that in your class there is a [Required] constraint on City or CityID. And suppose there are Person records without a (valid) City. The only way to satisfy the [Required] is to perform an inner join.

    But as long as the constraints in your Db and model match (ie CityID INT NOT NULL) it wouldn't really matter what kind of Join is used. That should be the normal case.

    And without the constraint you would of course expect a Left Join.

    0 讨论(0)
  • 2020-12-30 21:47

    I know this is an old question, but if anyone else lands here using EF Code First as I am, my issue was in the fluent mappings:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<Parent>()
                .HasOptional(a => a.Child) /* LEFT OUTER JOIN */
                .WithMany()
                .HasForeignKey(a => a.ChildId);
        }
    

    is translated as a LEFT OUTER JOIN whereas

        modelBuilder.Entity<Parent>()
            .HasRequired(a => a.Child) /* INNER JOIN */
            .WithMany()
            .HasForeignKey(a => a.ChildId);
    

    is translated as an INNER JOIN.

    0 讨论(0)
提交回复
热议问题