Junior — Middle
What methods and practices do you apply when working with data models in your projects?
sobes.tech AI
Answer from AI
In C# projects, I usually apply the following methods and practices:
- Using ORM (e.g., Entity Framework) for convenient mapping of objects to database tables.
- Separating models into layers: Data Models, View Models, and Domain Models for better code organization.
- Applying Data Annotations or Fluent API for validation and schema configuration.
- Using DTOs (Data Transfer Objects) for data transfer between layers and services to hide internal model structures.
- Regularly applying database migrations to synchronize the model and schema.
Example of a simple model with Entity Framework:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
And the database context:
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
}