Skip to content

Commit 2b4d62e

Browse files
authored
Merge pull request #77 from WeihanLi/dev
10.0.0 preview 3
2 parents 99824aa + b6df422 commit 2b4d62e

File tree

4 files changed

+142
-2
lines changed

4 files changed

+142
-2
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ImplicitUsings>enable</ImplicitUsings>
88
<Nullable>enable</Nullable>
99
<CommonVersion>1.0.76</CommonVersion>
10-
<EFVersion>10.0.0-preview.2.25163.8</EFVersion>
10+
<EFVersion>10.0.0-preview.3.25171.6</EFVersion>
1111

1212
<PublishRepositoryUrl>true</PublishRepositoryUrl>
1313
<EmbedUntrackedSources>true</EmbedUntrackedSources>

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ See Releases/PRs for details
6363
## Support
6464

6565
Feel free to try and [create issues](https://github.com/WeihanLi/WeihanLi.EntityFramework/issues/new) if you have any questions or integration issues
66+
67+
## Usage
68+
69+
For detailed usage instructions, please refer to the [Usage Documentation](docs/Usage.md).

docs/Usage.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Usage
2+
3+
## Installation
4+
5+
To install the `WeihanLi.EntityFramework` package, you can use the NuGet Package Manager, .NET CLI, or PackageReference in your project file.
6+
7+
**.NET CLI**
8+
9+
```
10+
dotnet add package WeihanLi.EntityFramework
11+
```
12+
13+
## Configuration
14+
15+
To configure the `WeihanLi.EntityFramework` package, you need to add the necessary services to your `IServiceCollection` in the `Startup.cs` or `Program.cs` file.
16+
17+
```csharp
18+
public void ConfigureServices(IServiceCollection services)
19+
{
20+
services.AddDbContext<MyDbContext>(options =>
21+
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
22+
23+
services.AddEFRepository();
24+
services.AddEFAutoUpdateInterceptor();
25+
services.AddEFAutoAudit(builder =>
26+
{
27+
builder.WithUserIdProvider<CustomUserIdProvider>()
28+
.EnrichWithProperty("MachineName", Environment.MachineName)
29+
.WithStore<AuditConsoleStore>();
30+
});
31+
}
32+
```
33+
34+
## Examples
35+
36+
### Repository Pattern
37+
38+
The `WeihanLi.EntityFramework` package provides a repository pattern implementation for Entity Framework Core.
39+
40+
```csharp
41+
public class MyService
42+
{
43+
private readonly IEFRepository<MyDbContext, MyEntity> _repository;
44+
45+
public MyService(IEFRepository<MyDbContext, MyEntity> repository)
46+
{
47+
_repository = repository;
48+
}
49+
50+
public async Task<MyEntity> GetEntityByIdAsync(int id)
51+
{
52+
return await _repository.FindAsync(id);
53+
}
54+
55+
public async Task AddEntityAsync(MyEntity entity)
56+
{
57+
await _repository.InsertAsync(entity);
58+
}
59+
60+
public async Task UpdateEntityAsync(MyEntity entity)
61+
{
62+
await _repository.UpdateAsync(entity);
63+
}
64+
65+
public async Task DeleteEntityAsync(int id)
66+
{
67+
await _repository.DeleteAsync(id);
68+
}
69+
}
70+
```
71+
72+
### Unit of Work Pattern
73+
74+
The `WeihanLi.EntityFramework` package also provides a unit of work pattern implementation for Entity Framework Core.
75+
76+
```csharp
77+
public class MyService
78+
{
79+
private readonly IEFUnitOfWork<MyDbContext> _unitOfWork;
80+
81+
public MyService(IEFUnitOfWork<MyDbContext> unitOfWork)
82+
{
83+
_unitOfWork = unitOfWork;
84+
}
85+
86+
public async Task SaveChangesAsync()
87+
{
88+
await _unitOfWork.CommitAsync();
89+
}
90+
}
91+
```
92+
93+
### Audit
94+
95+
The `WeihanLi.EntityFramework` package provides an audit feature to track changes in your entities.
96+
97+
```csharp
98+
public class MyDbContext : AuditDbContext
99+
{
100+
public MyDbContext(DbContextOptions<MyDbContext> options, IServiceProvider serviceProvider)
101+
: base(options, serviceProvider)
102+
{
103+
}
104+
105+
public DbSet<MyEntity> MyEntities { get; set; }
106+
}
107+
```
108+
109+
### Soft Delete
110+
111+
The `WeihanLi.EntityFramework` package provides a soft delete feature to mark entities as deleted without actually removing them from the database.
112+
113+
```csharp
114+
public class MyEntity : ISoftDeleteEntityWithDeleted
115+
{
116+
public int Id { get; set; }
117+
public string Name { get; set; }
118+
public bool IsDeleted { get; set; }
119+
}
120+
```
121+
122+
### Auto Update
123+
124+
The `WeihanLi.EntityFramework` package provides an auto update feature to automatically update certain properties, such as `CreatedAt`, `UpdatedAt`, `CreatedBy`, and `UpdatedBy`.
125+
126+
```csharp
127+
public class MyEntity : IEntityWithCreatedUpdatedAt, IEntityWithCreatedUpdatedBy
128+
{
129+
public int Id { get; set; }
130+
public string Name { get; set; }
131+
public DateTimeOffset CreatedAt { get; set; }
132+
public DateTimeOffset UpdatedAt { get; set; }
133+
public string CreatedBy { get; set; }
134+
public string UpdatedBy { get; set; }
135+
}
136+
```

test/WeihanLi.EntityFramework.Test/WeihanLi.EntityFramework.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<IsPackable>false</IsPackable>
66
<OutputType>exe</OutputType>
77
<Nullable>disable</Nullable>
8-
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
8+
<!-- <UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner> -->
99
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
1010
</PropertyGroup>
1111

0 commit comments

Comments
 (0)