Last active
October 13, 2016 19:19
-
-
Save joncloud/59d3834c854c663b929ec7836e4b6099 to your computer and use it in GitHub Desktop.
EF Core Joining
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestDbContext : DbContext | |
| { | |
| public DbSet<Child> Children { get; set; } | |
| public DbSet<Parent> Parents { get; set; } | |
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
| { | |
| base.OnConfiguring(optionsBuilder); | |
| optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=ConsoleApp12;Integrated Security=true;"); | |
| } | |
| } | |
| public class Parent | |
| { | |
| public int Id { get; set; } | |
| public string Name { get; set; } | |
| public List<Child> Children { get; set; } | |
| } | |
| public class Child | |
| { | |
| public int Id { get; set; } | |
| public Parent Parent { get; set; } | |
| public int ParentId { get; set; } | |
| public string Name { get; set; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Program | |
| { | |
| public static void Main(string[] args) | |
| { | |
| using (var context = new TestDbContext()) | |
| { | |
| context.Parents.Add(new Parent | |
| { | |
| Children = new List<Child> | |
| { | |
| new Child { Name = "Ipsum" }, | |
| new Child { Name = "Dolor" } | |
| }, | |
| Name = "Lorem" | |
| }); | |
| context.SaveChanges(); | |
| } | |
| using (var context = new TestDbContext()) | |
| { | |
| context.Parents.Where(parent => parent.Name == "Lorem") | |
| .SelectMany(parent => parent.Children) | |
| .Select(child => new Flat | |
| { | |
| ChildId = child.Id, | |
| ChildName = child.Name, | |
| ParentId = child.ParentId, | |
| ParentName = child.Parent.Name | |
| }) | |
| .ToList(); | |
| } | |
| } | |
| } | |
| public class Flat | |
| { | |
| public int ParentId { get; set; } | |
| public string ParentName { get; set; } | |
| public int ChildId { get; set; } | |
| public string ChildName { get; set; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "version": "1.0.0-*", | |
| "buildOptions": { | |
| "emitEntryPoint": true | |
| }, | |
| "dependencies": { | |
| "Microsoft.NETCore.App": { | |
| "type": "platform", | |
| "version": "1.0.1" | |
| }, | |
| "Microsoft.EntityFrameworkCore": "1.0.1", | |
| "Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final", | |
| "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1" | |
| }, | |
| "tools": { | |
| "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final" | |
| }, | |
| "frameworks": { | |
| "netcoreapp1.0": { | |
| "imports": "dnxcore50" | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT [parent.Children].[Id], [parent.Children].[Name], [parent.Children].[ParentId], [parent.Children.Parent].[Name] | |
| FROM [Parents] AS [parent] | |
| INNER JOIN [Children] AS [parent.Children] ON [parent].[Id] = [parent.Children].[ParentId] | |
| INNER JOIN [Parents] AS [parent.Children.Parent] ON [parent.Children].[ParentId] = [parent.Children.Parent].[Id] | |
| WHERE [parent].[Name] = N'Lorem' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment