static void

Entity Framework Core v1

EF Core v1 was released in 2016 and is a complete rewrite of Entity Framework (which dates back to v4.1 in 2011).
As of 2016, EF v6 Code First was still the recommended EF (EF Core v1.0.0 was missing some critical features, including lazy loading)

EF Core can run on .net Core (and thus not just on Windows), as well as the full framework. Some ADO providers only support the full framework (minimum 4.5.1).

Links

Nuget:

Configuring

You can't use app/web.config (because .net Core dumped it). If you forget to configure you get an InvalidOperationException with the message "No database provider has been configured for this DbContext...."

public class MyContext : DbContext
{
    public DbSet<ProductProducts { getset; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Integrated Security=true;Database=MyDatabase");
    }
}

Swapping from SqlServer to InMemory is a nice testing feature, but you'll have to inject options in tests with a constructor overload.

public class MyContext : DbContext
{
    public MyContext() { }

    public MyContext(DbContextOptions options) : base(options) { }

    public DbSet<ProductProducts { getset; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Integrated Security=true;Database=MyDatabase");
        }
    }
}

In the tests, use the ctor overload to inject your options.

var builder = new DbContextOptionsBuilder<MyContext>();
builder.UseInMemoryDatabase();
var options = builder.Options;
var context = new MyContext(options);

Database Initialization

In EF v6 we had context.Database.SetInitializer(new CreateDatabaseIfNotExists<ModelContext>());
and for migrations, Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());

In EF Core v1, use:

In v6 you could put this in OnModelCreating. In EF Core you can't; it needs to be in Startup.cs or equivalent.

Differences to EF v6

EF v6 EF Core v1
namespace System.Data.Entity namespace Microsoft.Data.Entity
app/web.config configuration (based on ADO DbProviderFactory) Code-based config
Database Initializers
Database.EnsureCreated(); or Database.Migrate();
DbContext.OnModelCreating(DbModelBuilder builder)
Split mapping into classes with EntityTypeConfiguration
DbContext.OnModelCreating(ModelBuilder builder)
No equivalent mapping classes- manually create static classes which take an EntityTypeBuilder
In fluent mapping, HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed) .ValueGeneratedOnAddOrUpdate()
also a .HasComputedColumnSql(sql)
In fluent mapping, IsMaxLength() No equivalent.
Missing features in EF Core v1
  • Lazy loading (big one for an ORM!)
  • Complex types
  • Lots of other things...