Adding Entities
LightNap makes use of Entity Framework (EF) for database access. The ApplicationDbContext
and supporting entities are all kept in the Data
folder of the LightNap.Core
project.
Adding a New Entity
The process for changing the EF model by adding, updating, or removing is straightforward. Just add/edit/remove classes and references and generate a migration.
-
Add the Entity Class: Create a new class in the
Entities
folder that represents the new entity.namespace LightNap.Core.Data.Entities { public class TestEntity { public int Id { get; set; } public string Name { get; set; } public int Quantity { get; set; } public double Price { get; set; } } }
-
Update DbContext: Add
DbSet<TEntity>
properties to theApplicationDbContext
class for each new entity.public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string> { public DbSet<TestEntity> TestEntities { get; set; } = null!;
- Configure Relationships: Define relationships and constraints using Fluent API or data annotations in your entity classes.
-
Migrations: Use EF migrations to update the database schema.
It’s recommended to use the in-memory database provider while working out the exact details of the entity. Once the model is finalized a single migration can be generated to keep the process cleaner.
Once the new entity is wired up, check out our scaffolding support to quickly generate code to expose that entity all the way through to the Angular app.