Created
June 20, 2019 08:18
-
-
Save adams85/9652225887c4de88144f31d77d4eb2a0 to your computer and use it in GitHub Desktop.
LinqToDB-EF Core integration
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
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using LinqToDB; | |
using LinqToDB.EntityFrameworkCore; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.DependencyInjection; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
class Category | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public ISet<CategoryItem> Items { get; set; } | |
} | |
class CategoryItem | |
{ | |
public int CategoryId { get; set; } | |
public Category Category { get; set; } | |
public int ItemId { get; set; } | |
public Item Item { get; set; } | |
} | |
class Item | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public ISet<CategoryItem> Categories { get; set; } | |
} | |
class DataContext : DbContext | |
{ | |
public DataContext(DbContextOptions options) : base(options) | |
{ | |
} | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
base.OnModelCreating(modelBuilder); | |
modelBuilder.Entity<CategoryItem>() | |
.HasKey(entity => new { entity.CategoryId, entity.ItemId }); | |
} | |
public DbSet<Category> Categories { get; set; } | |
public DbSet<CategoryItem> CategoryItems { get; set; } | |
public DbSet<Item> Items { get; set; } | |
public override void Dispose() | |
{ | |
base.Dispose(); | |
Debug.WriteLine("Disposed"); | |
} | |
} | |
static async Task Main(string[] args) | |
{ | |
LinqToDBForEFTools.Initialize(); | |
LinqToDB.Data.DataConnection.TurnTraceSwitchOn(); | |
LinqToDB.Data.DataConnection.WriteTraceLine = (message, displayName) => Debug.WriteLine($"{message} {displayName}"); | |
var services = new ServiceCollection(); | |
var optionsBuilder = new DbContextOptionsBuilder<DataContext>() | |
.UseSqlServer("Server=localhost;Database=Test;Integrated Security=SSPI;MultipleActiveResultSets=True", o => o.UseRowNumberForPaging()); | |
services.AddSingleton(optionsBuilder.Options); | |
services.AddDbContext<DataContext>(); | |
using (var sp = services.BuildServiceProvider()) | |
{ | |
using (var scope = sp.CreateScope()) | |
{ | |
var ctx = scope.ServiceProvider.GetRequiredService<DataContext>(); | |
await ctx.Database.EnsureCreatedAsync(); | |
var item1 = new Item | |
{ | |
Name = "ItemA", | |
}; | |
var item2 = new Item | |
{ | |
Name = "ItemB", | |
}; | |
var category1 = new Category | |
{ | |
Name = "Cat1", | |
Items = new HashSet<CategoryItem> | |
{ | |
new CategoryItem { Item = item1 }, | |
new CategoryItem { Item = item2 } | |
} | |
}; | |
var category2 = new Category | |
{ | |
Name = "Cat2", | |
Items = new HashSet<CategoryItem> | |
{ | |
new CategoryItem { Item = item2 } | |
} | |
}; | |
await ctx.AddRangeAsync(category1, category2); | |
await ctx.SaveChangesAsync(); | |
} | |
using (var scope = sp.CreateScope()) | |
{ | |
var ctx = scope.ServiceProvider.GetRequiredService<DataContext>(); | |
var q = | |
from cat in ctx.Categories | |
from catItem in ctx.CategoryItems.LeftJoin(ci => ci.CategoryId == cat.Id) | |
select new { catItem.Item.Name }; | |
var r = await q.ToArrayAsyncLinqToDB(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment