Skip to content

Commit b84c19d

Browse files
committed
updated
1 parent 655cba9 commit b84c19d

4 files changed

Lines changed: 33 additions & 13 deletions

File tree

F2.Repository.Demo/Startup.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
using F2.Repository.Demo.Mapper;
2+
using F2.Repository.Demo.Models;
3+
using F2.Repository.Extensions;
14
using Microsoft.AspNetCore.Builder;
25
using Microsoft.AspNetCore.Hosting;
36
using Microsoft.EntityFrameworkCore;
47
using Microsoft.Extensions.Configuration;
58
using Microsoft.Extensions.DependencyInjection;
6-
using F2.Repository.Demo.Mapper;
7-
using F2.Repository.Demo.Models;
8-
using F2.Repository.Extensions;
99

1010
namespace F2.Repository.Demo;
1111

@@ -16,10 +16,10 @@ public class Startup(IConfiguration configuration)
1616
// This method gets called by the runtime. Use this method to add services to the container.
1717
public void ConfigureServices(IServiceCollection services)
1818
{
19-
services.AddHostedService<HostedDbMigrationService<LibraryContext>>();
19+
services.AddDatabaseMigration<LibraryContext>();
2020

2121
var connectionString = _configuration.GetConnectionString("Demo");
22-
services.AddDbContext<LibraryContext>(config => config.UseNpgsql(connectionString));
22+
services.AddDbContextFactory<LibraryContext>(config => config.UseNpgsql(connectionString));
2323
services.AddDatabaseScope<LibraryDbScope>();
2424

2525
// https://github.com/AutoMapper/AutoMapper.Extensions.Microsoft.DependencyInjection
@@ -30,8 +30,6 @@ public void ConfigureServices(IServiceCollection services)
3030
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
3131
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3232
{
33-
//app.UseDatabaseMigration<LibraryContext>(); // relational databases only
34-
3533
app.UseRouting();
3634
app.UseEndpoints(endpoints => endpoints.MapControllers());
3735
}

F2.Repository/Abstracts/RepositoryBase.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,28 @@ public virtual EntityEntry<TEntity> Update(TEntity entity)
3636
}
3737

3838
/// <summary>
39-
/// <inheritdoc cref="Microsoft.EntityFrameworkCore.DbSet{TEntity}.Remove(TEntity)"/>
39+
/// Marks multiple entities for deletion in the current context.
40+
/// Untracked entities are automatically attached to the context.
4041
/// </summary>
41-
public virtual EntityEntry<TEntity> Remove(TEntity entity)
42+
/// <param name="entities">Array of entities to remove</param>
43+
/// <exception cref="ArgumentNullException">When entities array is null</exception>
44+
public virtual void RemoveRange(params TEntity[] entities)
4245
{
43-
if (_context.Entry(entity).State == EntityState.Detached)
46+
ArgumentNullException.ThrowIfNull(entities);
47+
48+
if (!entities.Any())
4449
{
45-
Set.Attach(entity);
50+
return;
4651
}
4752

48-
return Set.Remove(entity);
53+
// Filter and attach detached entities
54+
var detachedEntities = entities
55+
.Where(e => _context.Entry(e).State == EntityState.Detached)
56+
.ToArray();
57+
58+
if (detachedEntities.Any())
59+
{
60+
Set.AttachRange(detachedEntities);
4961
}
5062

5163
public virtual void RemoveRange(params TEntity[] entities)

F2.Repository/Extensions/HostedDbMigrationService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
namespace F2.Repository.Extensions;
44

5+
public static class DatabaseMigrationExtensions
6+
{
7+
public static IServiceCollection AddDatabaseMigration<TContext>(this IServiceCollection services)
8+
where TContext : DbContext
9+
{
10+
services.AddHostedService<HostedDbMigrationService<TContext>>();
11+
12+
return services;
13+
}
14+
}
15+
516
public class HostedDbMigrationService<TContext> : IHostedService
617
where TContext : DbContext
718
{

F2.Repository/F2.Repository.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2121
<TargetFramework>net8.0</TargetFramework>
2222
<Title>F2.Repository</Title>
23-
<UserSecretsId>320f5e09-36cb-4c5b-8f02-72cbf1003ad1</UserSecretsId>
2423
<Version>8.0.12</Version>
2524
<OutputType>Library</OutputType>
2625
<IsPackable>true</IsPackable>

0 commit comments

Comments
 (0)