-
-
Notifications
You must be signed in to change notification settings - Fork 0
Sample SubSonic.DbContext
kccarter76 edited this page Jun 28, 2020
·
8 revisions
You will need the SubSonic.Core and SubSonic.Extensions.SqlServer nuget packages.
using Microsoft.Extensions.DependencyInjection;
using SubSonic;
using SubSonic.Infrastructure;
using SubSonic.Extensions;
using SubSonic.Extensions.SqlServer;
namespace TheDataLayer
{
public class DataContext
: SubSonic.DbContext
{
private readonly IServiceCollection services = null;
public DataContext(IServiceCollection services)
: base()
{
this.services = services;
}
public DbSetCollection<Models.RealEstateProperty> RealEstateProperties { get; private set; }
public DbSetCollection<Models.Status> Statuses { get; private set; }
public DbSetCollection<Models.Unit> Units { get; private set; }
public DbSetCollection<Models.Renter> Renters { get; private set; }
public DbSetCollection<Models.Person> People { get; private set; }
protected override void OnDbConfiguring(DbContextOptionsBuilder config)
{
config
.ConfigureServiceCollection(services)
.UseSqlClient((config, options) =>
{
config
.SetDatasource("(localdb)\\MSSQLLocalDB")
.SetInitialCatalog("DbSubSonic")
.SetIntegratedSecurity(true);
});
}
protected override void OnDbModeling(DbModelBuilder builder)
{
builder.AddEntityModel<Models.RealEstateProperty>();
builder.AddEntityModel<Models.Status>();
builder.AddEntityModel<Models.Unit>();
builder.AddEntityModel<Models.Renter>();
builder.AddEntityModel<Models.Person>();
builder.AddRelationshipFor<Models.RealEstateProperty>(() =>
builder.GetRelationshipFor<Models.RealEstateProperty>()
.HasMany(Model => Model.Units)
.WithOne(Model => Model.RealEstateProperty));
builder.AddRelationshipFor<Models.RealEstateProperty>(() =>
builder.GetRelationshipFor<Models.RealEstateProperty>()
.HasOne(Model => Model.Status)
.WithOne());
builder.AddRelationshipFor<Models.Unit>(() =>
builder.GetRelationshipFor<Models.Unit>()
.HasOne(Model => Model.RealEstateProperty)
.WithMany(Model => Model.Units));
builder.AddRelationshipFor<Models.Unit>(() =>
builder.GetRelationshipFor<Models.Unit>()
.HasMany(Model => Model.Renters)
.WithMany(Model => Model.Person));
builder.AddRelationshipFor<Models.Unit>(() =>
builder.GetRelationshipFor<Models.Unit>()
.HasOne(Model => Model.Status)
.WithOne());
builder.AddRelationshipFor<Models.Person>(() =>
builder.GetRelationshipFor<Models.Person>()
.HasMany(Model => Model.Renters)
.WithMany(Model => Model.Unit));
}
}
}