update db from entity framework bool value code example
Example 1: entity framework update database automatically
public static IHost MigrateDbContext<TContext>(
this IHost host,
Action<TContext, IServiceProvider> seeder = null)
where TContext : DbContext
{
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var logger = services.GetRequiredService<ILogger<TContext>>();
var context = services.GetService<TContext>();
try
{
logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}");
var retry = Policy.Handle<Exception>().WaitAndRetry(new[]
{
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(15),
});
retry.Execute(() =>
{
context.Database.Migrate();
});
logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}");
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}");
}
}
return host;
}
Example 2: Update data in db .net
var query =
from ord in db.Orders
where ord.OrderID == 11000
select ord;
foreach (Order ord in query)
{
ord.ShipName = "Mariner";
ord.ShipVia = 2;
}
try
{
db.SubmitChanges();
}
catch (Exception e)
{
Console.WriteLine(e);
}