Major rewrite
This commit is contained in:
29
SoilMoistureAPI/Data/SoilMoistureContext.cs
Normal file
29
SoilMoistureAPI/Data/SoilMoistureContext.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SoilMoistureAPI.Models;
|
||||
|
||||
namespace SoilMoistureAPI.Data
|
||||
{
|
||||
public class SoilMoistureContext : DbContext
|
||||
{
|
||||
public SoilMoistureContext(DbContextOptions<SoilMoistureContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public DbSet<SoilMoisture> SoilMoistures { get; set; }
|
||||
public DbSet<Device> Devices { get; set; } // New DbSet for Devices
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
// Configure the primary key for Device
|
||||
modelBuilder.Entity<Device>()
|
||||
.HasKey(d => d.DeviceId);
|
||||
|
||||
// Configure one-to-many relationship
|
||||
modelBuilder.Entity<Device>()
|
||||
.HasMany(d => d.SoilMoistures)
|
||||
.WithOne(s => s.Device)
|
||||
.HasForeignKey(s => s.DeviceId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user