I’m working on a few ASP.NET MVC projects which all require database functionality. Unfortunately, my hosting provider only gives me two SQL Server databases to work with, so I want to put the tables of multiple projects into a single database.
However, some of my tables are similarly named, so I might run into some issues. Thus, I’m trying to find a way to change the names of all the tables so that they reflect what application they belong to.
Currently, I have the following tables in Project A:
Table1Table2
In Project B, I have these tables:
Table1Table2
I would like to combine the tables into a single database:
ATable1ATable2BTable1BTable2
My Questions
- How can I automatically add a prefix to the names of all of the tables inside of a SQL Server database?
- Is it possible to have LINQ to SQL automatically map
Table1from my code intoATable1orBTable1(depending on what application it is)?
Put the tables in separate schemas, e.g.
I haven’t tested this myself, but I would be very surprised if LINQ to SQL (and any other ORM for that matter) didn’t store and correctly make use of the schema names.
If you write your own SQL commands manually, be aware that you have to quote the schema name on all objects unless it is the default schema for your current database user. This could cause a problem if (a) you have only been allocated one SQL Server login, and (b) you have a lot of existing SQL.
To move existing tables into a new schema, e.g. tables in
dbointoProjectA:To do an automated rename, you could write a simple loop: