How do you structure and model databases?
sobes.tech AI
Answer from AI
The following methods are commonly used for structuring and modeling databases in C#:
-
Using ORM (Object-Relational Mapping): for example, Entity Framework allows describing the database structure through classes and properties, simplifying data handling and migrations.
-
Designing the data schema: creating entity and relationship diagrams (ER diagrams) to understand the structure and relationships of tables.
-
Data normalization: dividing data into logical tables to eliminate redundancy and ensure integrity.
-
Using migrations: automatically applying changes to the database structure through code, which facilitates maintenance and updates.
Example with Entity Framework Core:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer("connection_string");
}
Thus, database modeling boils down to describing classes and their relationships, with ORM handling the creation and updating of tables in the database.