Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's new in EF Core 6.0 preview 1 #3092

Merged
merged 3 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
342 changes: 339 additions & 3 deletions entity-framework/core/what-is-new/ef-core-6.0/whatsnew.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public static class EntityTypeConfigurationAttributeSample
{
public static void Using_EntityTypeConfigurationAttribute()
{
Console.WriteLine($">>>> Sample: {nameof(Using_EntityTypeConfigurationAttribute)}");
Console.WriteLine();

Helpers.RecreateCleanDatabase();
Helpers.PopulateDatabase();

using var context = new BooksContext();

var book = context.Books.Single(e => e.Id == 1);

Console.WriteLine();
}

public static class Helpers
{
public static void RecreateCleanDatabase()
{
using var context = new BooksContext();

context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}

public static void PopulateDatabase()
{
using var context = new BooksContext();

context.Add(
new Book() { Isbn = "978-0-39-481823-8", Title = "What Do People Do All Day?" });

context.SaveChanges();
}
}

#region BookEntityType
[EntityTypeConfiguration(typeof(BookConfiguration))]
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public string Isbn { get; set; }
}
#endregion

#region BookConfiguration
public class BookConfiguration : IEntityTypeConfiguration<Book>
{
public void Configure(EntityTypeBuilder<Book> builder)
{
builder
.Property(e => e.Isbn)
.IsUnicode(false)
.HasMaxLength(22);
}
}
#endregion

#region DbContext
public class BooksContext : DbContext
{
public DbSet<Book> Books { get; set; }

//...
#endregion

private readonly bool _quiet;

public BooksContext(bool quiet = false)
{
_quiet = quiet;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.EnableSensitiveDataLogging()
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFCoreSample");

if (!_quiet)
{
optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

public static class InMemoryRequiredPropertiesSample
{
public static void Required_properties_validated_with_in_memory_database()
{
Console.WriteLine($">>>> Sample: {nameof(Required_properties_validated_with_in_memory_database)}");
Console.WriteLine();

using var context = new UserContext();

context.Add(new User());

try
{
context.SaveChanges();
}
catch (Exception e)
{
Console.WriteLine($"{e.GetType().FullName}: {e.Message}");
}

Console.WriteLine();
}

public static void Required_property_validation_with_in_memory_database_can_be_disabled()
{
Console.WriteLine($">>>> Sample: {nameof(Required_property_validation_with_in_memory_database_can_be_disabled)}");
Console.WriteLine();

using var context = new UserContextWithNullCheckingDisabled();

context.Add(new User());

context.SaveChanges();

Console.WriteLine();
}

#region UserEntityType
public class User
{
public int Id { get; set; }

[Required]
public string Username { get; set; }
}
#endregion

public class UserContext : DbContext
{
public DbSet<User> Users { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.LogTo(Console.WriteLine, new[] { InMemoryEventId.ChangesSaved })
.EnableSensitiveDataLogging()
.UseInMemoryDatabase("UserContext");
}
}

public class UserContextWithNullCheckingDisabled : DbContext
{
public DbSet<User> Users { get; set; }

#region OnConfiguring
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.LogTo(Console.WriteLine, new[] { InMemoryEventId.ChangesSaved })
.UseInMemoryDatabase("UserContextWithNullCheckingDisabled")
.EnableNullabilityCheck(false);
}
#endregion
}
}
16 changes: 16 additions & 0 deletions samples/core/Miscellaneous/NewInEFCore6/NewInEFCore6.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0-preview.1.21109.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0-preview.1.21109.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-preview.1.21109.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.0-preview.1.21109.4" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

public static class PrecisionAttributeSample
{
public static void Using_PrecisionAttribute()
{
Console.WriteLine($">>>> Sample: {nameof(Using_PrecisionAttribute)}");
Console.WriteLine();

Helpers.RecreateCleanDatabase();
Helpers.PopulateDatabase();

using var context = new BooksContext();

_ = context.Products.Single(e => e.Id == 1);

Console.WriteLine();
}

public static class Helpers
{
public static void RecreateCleanDatabase()
{
using var context = new BooksContext();

context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}

public static void PopulateDatabase()
{
using var context = new BooksContext();

context.Add(new Product { Price = 3.99m });

context.SaveChanges();
}
}

#region ProductEntityType
public class Product
{
public int Id { get; set; }

[Precision(precision: 10, scale: 2)]
public decimal Price { get; set; }
}
#endregion

public class BooksContext : DbContext
{
public DbSet<Product> Products { get; set; }

private readonly bool _quiet;

public BooksContext(bool quiet = false)
{
_quiet = quiet;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.EnableSensitiveDataLogging()
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFCoreSample");

if (!_quiet)
{
optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
}
}
}
20 changes: 20 additions & 0 deletions samples/core/Miscellaneous/NewInEFCore6/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

public class Program
{
public static void Main()
{
Console.WriteLine("Samples for _Accessing Tracked Entities_");
Console.WriteLine();

EntityTypeConfigurationAttributeSample.Using_EntityTypeConfigurationAttribute();
UnicodeAttributeSample.Using_UnicodeAttribute();
PrecisionAttributeSample.Using_PrecisionAttribute();
ToStringTranslationSample.Using_ToString_in_queries();
RandomFunctionSample.Call_EF_Functions_Random();
SparseColumnsSample.Use_sparse_columns();
InMemoryRequiredPropertiesSample.Required_properties_validated_with_in_memory_database();
InMemoryRequiredPropertiesSample.Required_property_validation_with_in_memory_database_can_be_disabled();
SqliteSamples.SavepointsApi();
}
}
90 changes: 90 additions & 0 deletions samples/core/Miscellaneous/NewInEFCore6/RandomFunctionSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

public static class RandomFunctionSample
{
public static void Call_EF_Functions_Random()
{
Console.WriteLine($">>>> Sample: {nameof(Call_EF_Functions_Random)}");
Console.WriteLine();

Helpers.RecreateCleanDatabase();
Helpers.PopulateDatabase();

using var context = new BooksContext();

#region Query
var users = context.Users.Where(u => u.Popularity == (int)(EF.Functions.Random() * 5.0) + 1).ToList();
#endregion

foreach (var user in users)
{
Console.WriteLine($" Found '{user.Username}' with popularity '{user.Popularity}'");
}

Console.WriteLine();
}

public static class Helpers
{
public static void RecreateCleanDatabase()
{
using var context = new BooksContext(quiet: true);

context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}

public static void PopulateDatabase()
{
using var context = new BooksContext(quiet: true);

context.AddRange(
new User { Popularity = 1, Username = "arthur" },
new User { Popularity = 2, Username = "wendy" },
new User { Popularity = 3, Username = "smokey" },
new User { Popularity = 4, Username = "alice" },
new User { Popularity = 5, Username = "mac" },
new User { Popularity = 1, Username = "baxter" },
new User { Popularity = 2, Username = "olive" },
new User { Popularity = 3, Username = "toast" });

context.SaveChanges();
}
}

#region UserEntityType
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public int Popularity { get; set; }
}
#endregion

public class BooksContext : DbContext
{
public DbSet<User> Users { get; set; }

private readonly bool _quiet;

public BooksContext(bool quiet = false)
{
_quiet = quiet;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.EnableSensitiveDataLogging()
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFCoreSample");

if (!_quiet)
{
optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
}
}
}
Loading