Skip to content

Instantly share code, notes, and snippets.

@StefanRiedmann
Created May 7, 2020 01:28
Show Gist options
  • Save StefanRiedmann/2942bd39224a54dfc6fe466590ac0cb0 to your computer and use it in GitHub Desktop.
Save StefanRiedmann/2942bd39224a54dfc6fe466590ac0cb0 to your computer and use it in GitHub Desktop.
GardenContext with view
public class GardenContext : DbContext
{
public DbSet<Garden> Gardens { get; set; }
public DbSet<Bed> Beds { get; set; }
public DbSet<Crop> Crops { get; set; }
public DbSet<CropAssignment> CropAssignments { get; set; }
public DbSet<GardenInfo> GardenInfos { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<GardenInfo>(d =>
{
d.HasKey("GardenId", "Year");
d.ToView("View_GardenInfos");
d.HasOne("Garden").WithMany("GardenInfos");
});
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer("Server=localhost,1433; Database=GardenDb1;User=sa; Password=1StrongPassword!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment