-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
What's new in EF Core 6.0 preview 1 (#3092)
- Loading branch information
1 parent
87f2f7b
commit f7809de
Showing
14 changed files
with
1,394 additions
and
3 deletions.
There are no files selected for viewing
391 changes: 388 additions & 3 deletions
391
entity-framework/core/what-is-new/ef-core-6.0/whatsnew.md
Large diffs are not rendered by default.
Oops, something went wrong.
95 changes: 95 additions & 0 deletions
95
samples/core/Miscellaneous/NewInEFCore6/EntityTypeConfigurationAttributeSample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
samples/core/Miscellaneous/NewInEFCore6/InMemoryRequiredPropertiesSample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
samples/core/Miscellaneous/NewInEFCore6/IsNullOrWhitespaceSample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
|
||
public static class IsNullOrWhitespaceSample | ||
{ | ||
public static void Translate_IsNullOrWhitespace() | ||
{ | ||
Console.WriteLine($">>>> Sample: {nameof(Translate_IsNullOrWhitespace)}"); | ||
Console.WriteLine(); | ||
|
||
Helpers.RecreateCleanDatabase(); | ||
Helpers.PopulateDatabase(); | ||
|
||
using var context = new BooksContext(); | ||
|
||
#region Query | ||
var users = context.Users.Where( | ||
e => string.IsNullOrWhiteSpace(e.FirstName) | ||
|| string.IsNullOrWhiteSpace(e.LastName)).ToList(); | ||
#endregion | ||
|
||
foreach (var user in users) | ||
{ | ||
Console.WriteLine($"Found '{user.FirstName}' '{user.LastName}'"); | ||
} | ||
|
||
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 | ||
{ | ||
FirstName = "Arthur" | ||
}, | ||
new User | ||
{ | ||
FirstName = "Arthur", | ||
LastName = "Vickers" | ||
}, | ||
new User | ||
{ | ||
FirstName = "", | ||
LastName = "Vickers" | ||
}, | ||
new User | ||
{ | ||
FirstName = " ", | ||
LastName = "Vickers" | ||
}, | ||
new User | ||
{ | ||
FirstName = "Arthur", | ||
LastName = "\t" | ||
}); | ||
|
||
context.SaveChanges(); | ||
} | ||
} | ||
|
||
#region ProductEntityType | ||
public class User | ||
{ | ||
public int Id { get; set; } | ||
|
||
public string FirstName { get; set; } | ||
public string LastName { 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 }); | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
samples/core/Miscellaneous/NewInEFCore6/NewInEFCore6.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
76 changes: 76 additions & 0 deletions
76
samples/core/Miscellaneous/NewInEFCore6/PrecisionAttributeSample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.