Junior — Middle
Can you explain what it means to perform CRUD operations in the context of working with databases?
sobes.tech AI
Answer from AI
CRUD is an acronym that stands for the four basic operations with data in a database:
- Create: adding a new record to the database.
- Read: retrieving data from the database.
- Update: modifying existing data.
- Delete: removing records from the database.
In the context of C#, these operations are implemented through interaction with the database, for example, using Entity Framework:
// Create
var user = new User { Name = "Ivan" };
dbContext.Users.Add(user);
dbContext.SaveChanges();
// Read
var existingUser = dbContext.Users.Find(userId);
// Update
existingUser.Name = "Peter";
dbContext.SaveChanges();
// Delete
dbContext.Users.Remove(existingUser);
dbContext.SaveChanges();
Thus, CRUD operations provide a complete data management cycle.