Implementing Soft Deletes with Entity Framework Core

  Рет қаралды 4,209

Bald. Bearded. Builder.

Bald. Bearded. Builder.

Күн бұрын

Пікірлер: 56
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
Have you ever implemented soft deletes in a production app? I've only had to do it a few times, but I really like the pattern and REALLY like the ability to have an "undo" built into the app.
@wayne_taylor
@wayne_taylor 8 ай бұрын
I have once in my own MVC app, I did a bool flag . The method used in the current work DB is a number which represents it's deleted 🙂
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
@@wayne_taylor How is the number used? Like a bool? So 1 = true? Or is it some kind of state. Like 2 = something, 3 = deleted?
@taconaut8276
@taconaut8276 7 ай бұрын
I also had a few projects with soft delete. In the current one, we use temporal tables to track changes.
@BaldBeardedBuilder
@BaldBeardedBuilder 7 ай бұрын
@taconaut8276 that makes sense. I've got a video in the works now that discusses using interceptors for audit tracking you might be interested in.
@taconaut8276
@taconaut8276 7 ай бұрын
Sweet, keep it up
@SpiderJack
@SpiderJack 8 ай бұрын
I like the new code view, seeing you type as it shows on the screen is "chef's kiss."
@wobuntu
@wobuntu 8 ай бұрын
Saw a few of your videos now and I have to say; pure gold. Immediately subscribed after the first one I watched.
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
@wobuntu thanks for your kind words! I’m glad you’re enjoying them.
@markohy9samy565
@markohy9samy565 2 ай бұрын
I am really learning new stuff from you man thanks ♥
@BaldBeardedBuilder
@BaldBeardedBuilder 2 ай бұрын
Thanks for watching!
@Zeblab42
@Zeblab42 7 ай бұрын
Nice, short, useful video. You have my attention mr bearded developer man.
@BaldBeardedBuilder
@BaldBeardedBuilder 7 ай бұрын
Thanks for the comment! Glad you liked it.
@Zeblab42
@Zeblab42 7 ай бұрын
All jokes aside. I really like your videos. They cover useful topics and in just the right amount of detail. I have consumed a lot of C# material from docs to books to videos, and you still provided great insights and ideas. How did I not come across such a neat soft delete solution in 3 years of research? Perhaps I have too much hair and not enough beard.
@BaldBeardedBuilder
@BaldBeardedBuilder 7 ай бұрын
@@Zeblab42 Keep coding. The hair will leave. 😁
@SandzakBob
@SandzakBob 8 ай бұрын
What about situation when we use soft delete on a table that has unique constraint on some attribute, etc. email. If we soft delete a record and try to add that record with same email, it would violate the constraint, since this deleted record is not really deleted. Is there some best practice for these kind of scenarios?
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
That's a REALLY good question. I don't know if there's a "best practice," but I can tell you what I've seen done in the past. If you have a field like that, you could modify the interceptor to also add some unique value after the actual value. Then in your "undo" functionality, remove it. Like "test@test.com-2048210482" where the number is random or a datetimestamp. Then you could just use a regex to remove that ending on "undo." There are probably even better ways, but that's a simple one.
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
Another option would be to leave the "email" field unchanged and on create, if that record exists, just "undelete" it.
@Eneong
@Eneong 8 ай бұрын
​@@BaldBeardedBuilder At first I used that way but encountered performance issues when inserting because of record exist check. In the end I found better way to deal with it. For the solution I used postgresql's partial index. In EF core it can be implemented using b.HasIndex(field name).HasFilter("DeletedAt is null") in model config. So deleted entities just were not included in the index. But not sure If every db provider support that feature. I think that case is quite important to be covered somewhere in description or in pinned comment.
@l.b76
@l.b76 8 ай бұрын
You can create a unique constraint for the email by also including the deleted_at column. With this setup you can create a new user that has the same email as a softDeleted user. Now, this may have other drawbacks, like for example, your requirements change and you need to restore these softDeleted users.
@rankarat
@rankarat 8 ай бұрын
Really useful, thanks
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
Glad it was helpful and thanks a ton for the comment!
@gerb1419
@gerb1419 Ай бұрын
@Bald. Bearded. Builder. Can you tell me how to design an EF scheme if I need to save large files (blobs) in the MS SQL database? Like 20-50 megabyte files. As far as I understand, FILESTREAM is not supported by EF ?
@BaldBeardedBuilder
@BaldBeardedBuilder 25 күн бұрын
Hi @gerb1419. Great question for our Discord server. Join us to chat software development and lots more. bbb.dev/discord
@banzooiebooie
@banzooiebooie 8 ай бұрын
Cool, great idea. Learned something even if I am a Java developer!
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
Come to the dark side! 😁
@RikinPatel13
@RikinPatel13 8 ай бұрын
Que.: You are using DateTimeOffset datatype and you are setting UTC time so it required to use DateTimeOffset to use here?
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
@RikinPatel13, nope not required. I just prefer DateTimeOffset versus DateTime. It was probably just habit to type that rather than DateTime. Cheers!
@Kimo.Codess
@Kimo.Codess 8 ай бұрын
I usually just override save changes within the data context. how is that different from the interceptor? what advantage does the interceptor bring?
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
@kimfom I'm doing a deep-dive video on interceptors this week. For one, there's a lot more of those "hooks" than just SaveChanges. Also, a huge benefit to me, is that I can inject interceptors with DI. (maybe that was a spoiler for next week. 😁)
@Kimo.Codess
@Kimo.Codess 8 ай бұрын
@@BaldBeardedBuilder yeah thank you I sure am looking forward to that 😊
@williamliu8985
@williamliu8985 8 ай бұрын
Never seen this EF technique before, I feel like I've become stronger! 😁
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
@williamliu8985, just wait until next week's deep-dive video on EF Core interceptors. You're going to feel like a superhero. And thanks for the comment!
@saleem.shaikh
@saleem.shaikh 8 ай бұрын
Subscribed
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
Welcome aboard @saleem.shaikh!
@JtendraShahani
@JtendraShahani 8 ай бұрын
Is this an extension you use to create new files. Sorry, I always drop and extension, font, or theme question.😊
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
Don't apologize, I love talking about setups, extensions, shortcuts, etc.! Ask away! To answer your question, VS Code has a CTRL + N shortcut to create a new editor window. Then, CTRL + S will open a save dialog where I can name the file and specify its location. For me, it's faster than using the New File dialog in Visual Studio and keeps my hands on the keyboard.
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
@JtendraShahani I answered here. 😁
@one.pouria786
@one.pouria786 8 ай бұрын
Is there any way to apply this query filter to all of my entities?
@michaelholloway3547
@michaelholloway3547 8 ай бұрын
I've done something like apply this logic to all entities that implement an interface... (like the ISoftDelete used in this video) but I haven't implemented it with EntityFramework.
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
I've seen ways to do it, but all of them felt "icky" compared to just adding it to the entities long-form. Some used reflection 🤮, others iterated through the entities and added them. Either way seems like it adds more overhead than I'd want at runtime versus the mental load for a developer to just write them out once.
@one.pouria786
@one.pouria786 8 ай бұрын
@@BaldBeardedBuilder I think we can rewrite reflector version with code generator
@sanglin9387
@sanglin9387 8 ай бұрын
soft delete okay but you need a snapshot before and after for auditing purposes
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
@sanglin9387 maybe. It depends on the requirements. No need to build something that isn't in the requirements. Arbitrarily deciding to add before/after auditing is going to add overhead that may not be needed. Totally a good idea to ask & get clarity before just doing it.
@sanglin9387
@sanglin9387 8 ай бұрын
@@BaldBeardedBuilder for accounting system we do before no need because lot of screen just for adjust for mistake user 🤣. new developer think must be prefect but reality end user very ... unthinkable
@SpiderJack
@SpiderJack 8 ай бұрын
Maybe it's just a "bullion" - you mean like soup? The captioning got confused on boolean. 🤣
@BaldBeardedBuilder
@BaldBeardedBuilder 8 ай бұрын
It's flavorful solution to preserving data.
@PankajNikam
@PankajNikam 8 ай бұрын
I think it's just the gold we are talking about to pay EF not to delete the value 😉😉
@iteospace
@iteospace Ай бұрын
IgnoreQueryFilters if you use many query filters. Its problem
@Krauttrooper
@Krauttrooper 8 ай бұрын
Don't forget GDPR you have to delete pii.
@casperhansen826
@casperhansen826 8 ай бұрын
In that case you could have a routine to actually delete records that are marked as deleted once in a while
Don't Make These Entity Framework Core Mistakes
8:48
Bald. Bearded. Builder.
Рет қаралды 10 М.
Entity Framework Core Interceptors
12:18
Code Maze
Рет қаралды 1,6 М.
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Should You Use a Class, Struct or Record in C#?
7:53
Bald. Bearded. Builder.
Рет қаралды 22 М.
Handle ASP.NET Core Exceptions Globally
9:33
Bald. Bearded. Builder.
Рет қаралды 10 М.
Deconstruction In C#: An Underrated Feature?
2:28
PhanxDEV
Рет қаралды 89
Should you Delete or Soft Delete?
6:38
CodeOpinion
Рет қаралды 11 М.
Making Entity Framework Core As Fast As Dapper
13:17
Nick Chapsas
Рет қаралды 91 М.
Testing Entity Framework Core Correctly in .NET
8:03
Nick Chapsas
Рет қаралды 33 М.
Should I Use IEnumerable, IQueryable, ICollection, or IList?
4:22
Bald. Bearded. Builder.
Рет қаралды 7 М.
One Trick To Improve EF Core Performance Using Query Splitting
9:52
Milan Jovanović
Рет қаралды 38 М.
10 Signs Your Software Project Is Heading For FAILURE
17:59
Continuous Delivery
Рет қаралды 45 М.