I'm designing a multi-tenant application using Entity Framework 6. A key requirement in this application is that each tenant is allowed to have customized tables. This means that one table with the same name may differ between schemas.
For example, one tenant may have a "People" table with: ID, FirstName, LastName, BirthDate.
Another tenant may have a "People" table with: ID, FirstName, LastnName, BirthDate, FavoriteColor.
When I design my model, this is what I have:
public class Person
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
This works for the first tenant, but the custom column in the second tenant will not get loaded. What I am hoping to do is something like this:
public class Person : Dictionary<string, object>
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
And then write a custom model mapper/builder that will place the table columns who don't have a matching property into the dictionary collection.
There are a couple problems with this:
Entity Framework creates a SELECT statement that lists all the properties/columns in the model. Somehow I would need to replace the list with * so that all the columns, even the ones EF doesn't know about, get returned.
I need to be able to intercept the result set, map the known columns to the model properties, and put the unknown columns into the dictionary.
Is any of this possible? Is there a better way to solve this problem?