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:
- Install-Package Microsoft.EntityFrameworkCore.InMemory (Testing)
- Install-Package Microsoft.EntityFrameworkCore.SqlServer
- Install-Package Microsoft.EntityFrameworkCore.SQLite
- Install-Package Npgsql.EntityFrameworkCore.PostgreSQL
- Install-Package Microsoft.EntityFrameworkCore.Tools -pre (Migrations, still not RTMed)
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 DbSet<Product> Products { get; set; }
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 MyContext() { }
public MyContext(DbContextOptions options) : base(options) { }
public DbSet<Product> Products { get; set; }
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.
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:
- For simple create-database: context.Database.EnsureCreated()
- For migrations, Add-Migration "name" and Database.Migrate()
NB: does not work with InMemory - "Unable to resolve service for type ...IMigrator". If you swap between InMemory and a database, you may have to code around this.
In v6 you could put this in OnModelCreating. In EF Core you can't; it needs to be in Startup.cs or equivalent.
- New command for reverse engineering a db:
Scaffold-DbContext "connection string" Microsoft.EntityFrameworkCore.SqlServer
- The EF v6 "Enable-Migrations" command has gone.
- The _MigrationHistory table is renamed to __EFMigrationsHistory (it is not created with "EnsureCreated()").
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
|