Logo

dev-resources.site

for different kinds of informations.

Delete in EF 8 !

Published at
9/4/2023
Categories
csharp
entityframework
ef
dotnet
Author
bugandfix
Categories
4 categories in total
csharp
open
entityframework
open
ef
open
dotnet
open
Author
9 person written this
bugandfix
open
Delete in EF 8 !

Deleting a record efficiently using Entity Framework (EF) involves a few key considerations to minimize unnecessary database operations and improve performance.


public async void DeleteStudent(int Id)
{
        UniverSityDBContext db = new UniverSityDBContext();
        var studentData = db.Students.FindAsync(Id).Result;                
        if (studentData == null)
            throw new Exception("Student not found");

        db.Students.Remove(studentData);
        await db.SaveChangesAsync();
}
//
public void DeleteStudentOptimized(int Id)
{
    UniverSityDBContext db = new UniverSityDBContext();
    var student = new Student { ID = Id };
    var studentEntity = db.Students.Attach(student);
    studentEntity.State = EntityState.Deleted;
    db.SaveChanges();
}
Enter fullscreen mode Exit fullscreen mode
entityframework Article's
30 articles in total
Favicon
Entity Framework Core Code First
Favicon
Code First Approach with Entity Framework.
Favicon
Custom NET8 Entity Framework Core Generic Repository
Favicon
Link Many To Many entities with shadow join-table using Entity Framework Core
Favicon
Running Entity Framework Core Migrations with Optimizely CMS 12
Favicon
Check Pagination in .NET: With and Without Entity Framework
Favicon
EF Core 6 - correct types halving the execution time!
Favicon
EF Core 6 - This SqlTransaction has completed; it is no longer usable.
Favicon
Entity Framework Core Tutorial:Introduction to Entity Framework Core
Favicon
ReadOnly DbContext with Entity Framework
Favicon
[KOSD] Multiple Parallel Operations in Entity Framework Core (.NET 8)
Favicon
Entity Framework in .net core 6.0 - Code first and Database first approach
Favicon
5 EF Core Features You Need To Know
Favicon
C# | Best Practices for Pagination using EF Core 8
Favicon
C# | Using Entity Framework with PostgreSQL Database
Favicon
C# | Entity Framework Generic Repository with SOLID Design Pattern
Favicon
C# | Entity Framework Issues and Troubleshooting
Favicon
Entity Framework Core with Scalar Functions
Favicon
Prefer Empty Objects over Compiler tricks
Favicon
The Differences Between EntityFramework .Add and .AddAsync
Favicon
Entity FrameWork
Favicon
Load Appointments on Demand in Blazor Scheduler using Entity Framework Core
Favicon
Finding the Right Balance: Clean Architecture and Entity Framework in Practice
Favicon
Compilation steps in EF Core
Favicon
Learning is another full-time job.
Favicon
How To Use EF Core Interceptors
Favicon
Using Entity Framework Core 8 Owned Types HasData()
Favicon
Simple Event-Sourcing with EF Core and SQL Server
Favicon
Delete in EF 8 !
Favicon
Optimizing Database Access with Entity Framework - Lazy Loading vs. Eager Loading

Featured ones: