EF4 LINQ Ordering Parent and all child collections with Eager Loading (.Include())

If you need to have Ordering or Filtering on inner navigation properties (e.g. Models) then you cannot eager load them using Include method anymore. Instead, you can use EntityCollection<TEntity>.CreateSourceQuery Method like this: List years = db.Years.OrderBy(“it.Name”).ToList(); foreach(year in years) { var makesQuery = year.Makes.CreateSourceQuery().OrderBy(m => m.Name); year.Makes.Attach(makesQuery); foreach(make in year.Makes) { var modelsQuery = make.Models.CreateSourceQuery().OrderBy(m … Read more

Entity Framework Core 2.0 – Run migrations step by step

You can use the GetPendingMigrations extension method of the DatabaseFacade class (returned by Database property of the DbContext) to get the list of the pending migration names. Then you can obtain IMigrator service and use Migrate method passing each target migration name: using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; DbContext db = …; var pendingMigrations = … Read more

.NET 4.5 Beta DbGeography NotImplementedException

DefaultSpatialServices in Entity Framework are using SqlGeography and SqlGeometry types as backing types. These two types live in Microsoft.SqlServer.Types.dll assembly that is not part of the .NET Framework. The exception is thrown when EF cannot find these types (the exception could be more helpful…). When you install Visual Studio it will install localdb on your … Read more

Why does EF5 code first use datetime2 when inserting a nullable datetime into the database?

The DateTime type in .NET has the same range and precision as datetime2 in SQL Server. When EF inserts or updates a datetime or datetime2 column in SQL Server it converts the model property to the type that can hold the whole range of DateTime in .NET, that’s datetime2. Converting into datetime would fail if … Read more

Entity Framework Code First Many to Many Setup For Existing Tables

Remove your Essence2EssenceSet model class. If junction table contains only keys of related entities participating in many-to-many relations it is not needed to map it as entity. Also make sure that your fluent mapping of many-to-many relations specifies schema for table: mb.Entity<Essence>() .HasMany(e => e.EssenceSets) .WithMany(set => set.Essences) .Map(mc => { mc.ToTable(“Essence2EssenceSet”, “Com”); mc.MapLeftKey(“EssenceID”); mc.MapRightKey(“EssenceSetID”); … Read more

LINQ To Entities + Include + Anonymous type issue

As Ladislav mentioned, Include only works if you select the Ticket entity directly. Since you’re projecting other information out, the Include gets ignored. This should provide a good work-around: var data = ctx.Set<Ticket>() .Select(p => new { Ticket = p, Clients = p.Client, LastReplyDate = p.Replies.Max(q => q.DateCreated) }); First of all, each Ticket’s Clients … Read more