EF 6 | EF CORE |
---|---|
Open-source | Open-source |
First released - 2008 with .NET Framework 3.5 SP1 |
First realeased - June 2016 with .NET Core 1.0 |
Stable and feature rich | New and evolving |
Windows only | Windows, Linux, OSX |
Works on .NET Framework 3.5+ | Works on .NET Framework 4.5+ and .NET Core |
THE DIFFERENCES BETWEEN EF6 AND EF CORE
VERSION | .NET FRAMEWORK |
---|---|
EF Core 2.0 | .NET Core 2.0 |
EF Core 1.1 | .NET Core 1.1 |
EF Core 1.0 | .NET Core 1.0 |
DATABASE | NuGet PACKAGE |
---|---|
SQL SERVER | Microsoft.EntityFrameworkCore.SqlServer |
MySQL | MySql.Data.EntityFrameworkCore |
PostgreSQL | Npgsql.EntityFrameworkCore.PostgreSQL |
SQLite | Microsoft.EntityFrameworkCore.SQLite |
SQL Compact | EntityFrameworkCore.SqlServerCompact40 |
In-memory | Microsoft.EntityFrameworkCore.InMemory |
public class SchoolContext : DbContext
{
public SchoolContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
//entities
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
}
Use fluent API
Grouping
Use data annotations
using Microsoft.EntityFrameworkCore;
namespace EFModeling.FluentAPI.Required
{
internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
#region Required
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.IsRequired();
}
#endregion
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
}
public class BlogEntityTypeConfiguration : IEntityTypeConfiguration<Blog>
{
public void Configure(EntityTypeBuilder<Blog> builder)
{
builder
.Property(b => b.Url)
.IsRequired();
}
}
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
namespace EFModeling.DataAnnotations.Required
{
internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
}
#region Required
public class Blog
{
public int BlogId { get; set; }
[Required]
public string Url { get; set; }
}
#endregion
}