Skip to content

Instantly share code, notes, and snippets.

@dcomartin dcomartin/program.cs
Last active Nov 10, 2017

Embed
What would you like to do?
class Program
{
static void Main(string[] args)
{
var tenant1 = Guid.NewGuid();
var tenant2 = Guid.NewGuid();
int customerId = 1;
Seed(tenant1, customerId, "Derek Comartin");
Seed(tenant2, customerId, "CodeOpinion.com");
using (var db = DbFactory(tenant1))
{
var customer = db.Customers.Single(x => x.CustomerId == customerId);
Console.WriteLine($"Hello {customer.Name}");
}
using (var db = DbFactory(tenant2))
{
var customer = db.Customers.Single(x => x.CustomerId == customerId);
Console.WriteLine($"Hello {customer.Name}");
}
Console.ReadKey();
}
private static MyDbContext DbFactory(Guid tenantId)
{
var connection = @"Server=(localdb)\mssqllocaldb;Database=EFCoreMultiTenant;Trusted_Connection=True;";
return new MyDbContext(tenantId, connection);
}
static void Seed(Guid tenantId, int customerId, string name)
{
using (var db = DbFactory(tenantId))
{
db.Customers.Add(new Customer
{
CustomerId = customerId,
TenantId = tenantId,
Name = name
});
db.SaveChanges();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.