I'm creating an ASP.NET MVC3 project in C# that is using an already existing SQL Server database.
The SQL Server has different databases, one for each customer. Each customer database has numerous tables, but I'm interested in only one table.
So, I'm interested in retrieving (and not updating or deleting) d开发者_StackOverflow中文版ata from the same table of every customer. This is the database configuration example:
DatabaseCustomerName1 - TableNeeded DatabaseCustomerName2 - TableNeeded DatabaseCustomerNameN - TableneededThe question is... how do I create the model?
Knowing that If I had to do it for just one customer I would create the model basing on the fields of the tables, how can I manage the situation of having multiple customers?
Thanks in advance.
Attila
You should have an interface for Repository class for example and different implementations for each custumer data base
internal interface IProductRepository
{
IEnumerable<Product> GetAll();
}
class ProductRepositoryCustumerOne : IProductRepository
{
public IEnumerable<Product> GetAll()
{
//code to retrieve data
}
}
class ProductRepositoryCustumerTwo : IProductRepository
{
public IEnumerable<Product> GetAll()
{
//code to retrieve data
}
}
after you can inject with IoC container what implementation you need
Let me just clarify your question, are you saying you've got a number of customers with a database each?
So...
Databases
- Customer A
- Customer B
And for each of those customers you're saying you have a table that you want to access so you have
- Customer A > TheTable
- Customer B > TheTable
So you're asking, how can you create a Model that will work for any customer to access TheTable?
Is that correct?
What about creating a Sql View over all tables based on a select link this:
Select 'Customer 1' as Customer, c.* from FirstDb.dbo.Cusotmers c
Union Select 'Customer 2' as Customer, c.* from SecondDb.dbo.Cusotmers c
Union ...
Than you can handel this view with e.g. EF or linq to sql.
Is it safe to assume you are using Entity Framework? Is the schema in all the databases the same?
You should be able to create your model against one of the databases. Then whenever you want to select a different customer you will need to change the Initial Catalog (or equivalent) value in the connection string before you create your data context.
I don't have any code handy at the moment. Does this help?
精彩评论