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

Some using declarations #3795

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 7 additions & 9 deletions samples/core/DbContextPooling/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,14 @@ await Task.WhenAll(

private static void SetupDatabase(IServiceProvider serviceProvider)
{
using (var serviceScope = serviceProvider.CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<BloggingContext>();
using var serviceScope = serviceProvider.CreateScope();
var context = serviceScope.ServiceProvider.GetService<BloggingContext>();

if (context.Database.EnsureCreated())
{
context.Blogs.Add(new Blog { Name = "The Dog Blog", Url = "http://sample.com/dogs" });
context.Blogs.Add(new Blog { Name = "The Cat Blog", Url = "http://sample.com/cats" });
context.SaveChanges();
}
if (context.Database.EnsureCreated())
{
context.Blogs.Add(new Blog { Name = "The Dog Blog", Url = "http://sample.com/dogs" });
context.Blogs.Add(new Blog { Name = "The Cat Blog", Url = "http://sample.com/cats" });
context.SaveChanges();
}
}

Expand Down
51 changes: 25 additions & 26 deletions samples/core/GetStarted/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@ internal class Program
{
private static void Main()
{
using (var db = new BloggingContext())
{
// Note: This sample requires the database to be created before running.
Console.WriteLine($"Database path: {db.DbPath}.");

// Create
Console.WriteLine("Inserting a new blog");
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
db.SaveChanges();
using var db = new BloggingContext();

// Read
Console.WriteLine("Querying for a blog");
var blog = db.Blogs
.OrderBy(b => b.BlogId)
.First();
// Note: This sample requires the database to be created before running.
Console.WriteLine($"Database path: {db.DbPath}.");

// Update
Console.WriteLine("Updating the blog and adding a post");
blog.Url = "https://devblogs.microsoft.com/dotnet";
blog.Posts.Add(
new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" });
db.SaveChanges();
// Create
Console.WriteLine("Inserting a new blog");
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
db.SaveChanges();

// Delete
Console.WriteLine("Delete the blog");
db.Remove(blog);
db.SaveChanges();
}
// Read
Console.WriteLine("Querying for a blog");
var blog = db.Blogs
.OrderBy(b => b.BlogId)
.First();

// Update
Console.WriteLine("Updating the blog and adding a post");
blog.Url = "https://devblogs.microsoft.com/dotnet";
blog.Posts.Add(
new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" });
db.SaveChanges();

// Delete
Console.WriteLine("Delete the blog");
db.Remove(blog);
db.SaveChanges();
}
}
}
136 changes: 65 additions & 71 deletions samples/core/Miscellaneous/ConnectionResiliency/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,101 +27,95 @@ public static void Main(string[] args)
private static void ExecuteWithManualTransaction()
{
#region ManualTransaction
using (var db = new BloggingContext())
{
var strategy = db.Database.CreateExecutionStrategy();

strategy.Execute(
() =>
{
using (var context = new BloggingContext())
{
using (var transaction = context.Database.BeginTransaction())
{
context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" });
context.SaveChanges();

context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/visualstudio" });
context.SaveChanges();

transaction.Commit();
}
}
});
}

using var db = new BloggingContext();
var strategy = db.Database.CreateExecutionStrategy();

strategy.Execute(
() =>
{
using var context = new BloggingContext();
using var transaction = context.Database.BeginTransaction();

context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" });
context.SaveChanges();

context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/visualstudio" });
context.SaveChanges();

transaction.Commit();
});

#endregion
}

private static void ExecuteWithManualAmbientTransaction()
{
#region AmbientTransaction
using (var context1 = new BloggingContext())
{
context1.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/visualstudio" });

var strategy = context1.Database.CreateExecutionStrategy();

strategy.Execute(
() =>
{
using (var context2 = new BloggingContext())
{
using (var transaction = new TransactionScope())
{
context2.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" });
context2.SaveChanges();

context1.SaveChanges();

transaction.Complete();
}
}
});
}

using var context1 = new BloggingContext();
context1.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/visualstudio" });

var strategy = context1.Database.CreateExecutionStrategy();

strategy.Execute(
() =>
{
using var context2 = new BloggingContext();
using var transaction = new TransactionScope();

context2.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" });
context2.SaveChanges();

context1.SaveChanges();

transaction.Complete();
});

#endregion
}

private static void ExecuteInTransactionWithVerification()
{
#region Verification
using (var db = new BloggingContext())
{
var strategy = db.Database.CreateExecutionStrategy();

var blogToAdd = new Blog { Url = "http://blogs.msdn.com/dotnet" };
db.Blogs.Add(blogToAdd);
using var db = new BloggingContext();
var strategy = db.Database.CreateExecutionStrategy();

strategy.ExecuteInTransaction(
db,
operation: context => { context.SaveChanges(acceptAllChangesOnSuccess: false); },
verifySucceeded: context => context.Blogs.AsNoTracking().Any(b => b.BlogId == blogToAdd.BlogId));
var blogToAdd = new Blog { Url = "http://blogs.msdn.com/dotnet" };
db.Blogs.Add(blogToAdd);

strategy.ExecuteInTransaction(
db,
operation: context => { context.SaveChanges(acceptAllChangesOnSuccess: false); },
verifySucceeded: context => context.Blogs.AsNoTracking().Any(b => b.BlogId == blogToAdd.BlogId));

db.ChangeTracker.AcceptAllChanges();

db.ChangeTracker.AcceptAllChanges();
}
#endregion
}

private static void ExecuteInTransactionWithTracking()
{
#region Tracking
using (var db = new BloggingContext())
{
var strategy = db.Database.CreateExecutionStrategy();

db.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" });
using var db = new BloggingContext();
var strategy = db.Database.CreateExecutionStrategy();

var transaction = new TransactionRow { Id = Guid.NewGuid() };
db.Transactions.Add(transaction);
db.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" });

strategy.ExecuteInTransaction(
db,
operation: context => { context.SaveChanges(acceptAllChangesOnSuccess: false); },
verifySucceeded: context => context.Transactions.AsNoTracking().Any(t => t.Id == transaction.Id));
var transaction = new TransactionRow { Id = Guid.NewGuid() };
db.Transactions.Add(transaction);

strategy.ExecuteInTransaction(
db,
operation: context => { context.SaveChanges(acceptAllChangesOnSuccess: false); },
verifySucceeded: context => context.Transactions.AsNoTracking().Any(t => t.Id == transaction.Id));

db.ChangeTracker.AcceptAllChanges();
db.Transactions.Remove(transaction);
db.SaveChanges();

db.ChangeTracker.AcceptAllChanges();
db.Transactions.Remove(transaction);
db.SaveChanges();
}
#endregion
}
}
Expand Down Expand Up @@ -156,4 +150,4 @@ public class Blog
public class TransactionRow
{
public Guid Id { get; set; }
}
}
26 changes: 12 additions & 14 deletions samples/core/Miscellaneous/NewInEFCore6/SqliteSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,23 @@ public static void DateOnly_and_TimeOnly()
Helpers.RecreateCleanDatabase();
Helpers.PopulateDatabase();

using var context = new UsersContext();

using (var context = new UsersContext())
{
#region DateOnlyQuery
var users = context.Users.Where(u => u.Birthday < new DateOnly(1900, 1, 1)).ToList();
#endregion
#region DateOnlyQuery
var users = context.Users.Where(u => u.Birthday < new DateOnly(1900, 1, 1)).ToList();
#endregion

Console.WriteLine();
Console.WriteLine();

foreach (var user in users)
{
Console.WriteLine($" Found '{user.Username}'");
user.Birthday = new(user.Birthday.Year + 100, user.Birthday.Month, user.Birthday.Day);
}
foreach (var user in users)
{
Console.WriteLine($" Found '{user.Username}'");
user.Birthday = new(user.Birthday.Year + 100, user.Birthday.Month, user.Birthday.Day);
}

Console.WriteLine();
Console.WriteLine();

context.SaveChanges();
}
context.SaveChanges();
}

private static void PerformUpdates()
Expand Down
Loading