Entity Framework Core Lessons

Getting Id of last inserted Entity

If the entity is using a SQL server identity column as ID, this ID will be populated by EF after a call to SaveChanges.

See this thread on StackOverflow


  • One to Many TL;DR; use HasOne and WithOne:
// Code in a class that inherits from DbContext

protected override void OnModelCreating(ModelBuilder modelBuilder)
        .HasKey(parent => parent.ParentId);

        .Entity<ChildEntity>(builder =>
            builder.HasKey(details => details.MessageDetailId);
                .HasOne(msg => msg.ParentEntity)
                .WithMany(it => it.ChildEntity)
                .HasForeignKey(it => it.ForeignKeyId);

When queries cannot be evaluated server side

EF Core query could not be translated and will be evaluated locally https://stackoverflow.com/q/45237492/190476

The problem: EF Core could not generate SQL according to the LINQ expression provided. What is the downside: More data would be brought over from database than necessary as the query expression would be evaluated in client (C#) code

  • See also, the difference between First, FirstOrDefault, Single and SingleOrDefault