-
-
Notifications
You must be signed in to change notification settings - Fork 0
Sample SubSonic.DbContext
Kenneth Carter edited this page Jul 16, 2020
·
8 revisions
You will need the SubSonic.Core and SubSonic.Extensions.SqlServer nuget packages. This sample assumes that the service collection already exists and that logging has already been setup.
In the OnDbModeling method you can see examples on adding models to the DbEntityModel. There are also examples on how relationship mapping is done.
using Microsoft.Extensions.DependencyInjection;
using SubSonic;
using SubSonic.Extensions;
using SubSonic.Extensions.SqlServer;
using System;
namespace TheDataLayer
{
public class DataContext
: SubSonicContext
{
private readonly IServiceCollection services = null;
public DataContext(IServiceCollection services)
: base()
{
this.services = services ?? throw new ArgumentNullException(nameof(services));
}
public ISubSonicSetCollection<Models.RealEstateProperty> RealEstateProperties { get; private set; }
public ISubSonicSetCollection<Models.Status> Statuses { get; private set; }
public ISubSonicSetCollection<Models.Unit> Units { get; private set; }
public ISubSonicSetCollection<Models.Renter> Renters { get; private set; }
public ISubSonicSetCollection<Models.Person> People { get; private set; }
protected override void OnDbConfiguring(DbContextOptionsBuilder config)
{
config.SetServiceProvider(services.BuildServiceProvider());
config.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)
.WithOne(Model => Model.Unit));
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)
.WithOne(Model => Model.Person));
builder.AddRelationshipFor<Models.Person>(() =>
builder.GetRelationshipFor<Models.Person>()
.HasMany(Model => Model.Units)
.UsingLookup(Model => Model.Renters)
.WithMany());
}
}
}