Skip to content

Commit 40f4c82

Browse files
committed
Added EF.Exceptions sample
1 parent 3bf9a1a commit 40f4c82

File tree

7 files changed

+86
-0
lines changed

7 files changed

+86
-0
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using EntityFramework.Exceptions.Sqlite;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
public class BlogContext : DbContext
5+
{
6+
public DbSet<BlogPost> BlogPosts { get; set; } = default!;
7+
8+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
9+
{
10+
optionsBuilder
11+
.UseSqlite("Filename=:memory:")
12+
.UseExceptionProcessor();
13+
}
14+
15+
protected override void OnModelCreating(ModelBuilder modelBuilder)
16+
{
17+
modelBuilder.Entity<BlogPost>().Property(b => b.Title)
18+
.IsRequired();
19+
}
20+
}

EntityFrameworkExceptions/BlogPost.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class BlogPost
2+
{
3+
public int Id { get; set; }
4+
public string? Title { get; set; }
5+
public string Content { get; set; } = string.Empty;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="EntityFrameworkCore.Exceptions.Sqlite" Version="6.0.3" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.9" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkExceptions", "EntityFrameworkExceptions.csproj", "{E2827847-99BE-4314-BBB5-3A844ECC7ACE}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{E2827847-99BE-4314-BBB5-3A844ECC7ACE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{E2827847-99BE-4314-BBB5-3A844ECC7ACE}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{E2827847-99BE-4314-BBB5-3A844ECC7ACE}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{E2827847-99BE-4314-BBB5-3A844ECC7ACE}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

EntityFrameworkExceptions/Program.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using EntityFramework.Exceptions.Common;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
await using var context = new BlogContext();
5+
context.Database.OpenConnection();
6+
context.Database.EnsureCreated();
7+
8+
var blogPostWithNullTitle = new BlogPost
9+
{
10+
Title = null,
11+
Content = "Content"
12+
};
13+
14+
context.BlogPosts.Add(blogPostWithNullTitle);
15+
try
16+
{
17+
await context.SaveChangesAsync();
18+
}
19+
catch (CannotInsertNullException e)
20+
{
21+
// Do something specific to this exception
22+
Console.WriteLine(e);
23+
}

EntityFrameworkExceptions/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Gracefully Handling Entity Framework Exceptions with EntityFramework.Exceptions
2+
3+
Working with databases can sometimes be daunting, mainly when errors occur. These errors or exceptions can be due to many reasons, such as constraint violations, connection issues, or syntax errors. Entity Framework throws a generic DbException or DbUpdateException for most of these database issues. But we cand get more specific exceptions based on the concrete "problem"! That's where EntityFramework.Exceptions comes in.
4+
5+
Found [here](https://steven-giesel.com/blogPost/693e8b9c-4b97-43a4-8bf7-991c633900b4)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Contains all of my examples from various blog posts. You can find a comprehensiv
44

55
| BlogPost | Publish Date |
66
| -------------------------------------------------------------------------------------------------------------- | ------------ |
7+
| [Gracefully Handling Entity Framework Exceptions with EntityFramework.Exceptions](EntityFrameworkExceptions/) | 25.07.2023 |
78
| [Create your own Mediator (like Mediatr)](Mediator/) | 21.06.2023 |
89
| [Verifying your DI Container](ServiceCollectionVerify/) | 30.04.2023 |
910
| [.NET 8 Performance Edition](BenchmarkDotNet8/) | 12.04.2023 |

0 commit comments

Comments
 (0)