Database schema library
Is there any library for .NET that allows user to manipulate database schema from code using the same code for different DB providers?
I built my own utility library that allows me to create or alter tables or columns but I wonder if there is already such a library. One that also includes constraints and indexes.
I use something like this:
DBManager db = new DBMan开发者_开发问答ager(connString);
DBTable persons = new DBTable("Persons");
orders.Columns.Add(new DBColumn("Name", DBType.Varchar, 100);
orders.Columns.Add(new DBColumn("Birthday", DBType.Date);
db.CreateTable(persons);
db.AddColumn("Persons", new DBColumn("CityID",DBType.Int);
db.DropColumn("Persons", "Birthday");
Of course, this isn't hardcoded. I want to add a feature to my application to allow users to specify they own entities that can be stored in database and then my app will use the library to create the underlying database table.
I only know how to query database schema from code using the same code for different DB providers.
Since dotnet 2.0 most dotnet-providers implement System.Data.Common.DbConnection .
With the System.Data.Common.DbConnection.GetSchema()
function (without parameter) you can ask the provider
which collectionNames it supports (tables, views, stored procedures, .....)
These can be used with GetSchema(collectionName)
to get more details. Among the details are even supported datatypes and regular expressions to validate fieldnames.
You can also look at the MyGeneration project source . Its mymeta engine builds a common api around different databaseproviders.
NHibernate must have something like a common database-provider as well. But i havent looked at its sourcecode yet.
You can use the SQL Server Management Objects http://msdn.microsoft.com/en-us/library/ms162169.aspx
If it is SQL Server you are working with of course...
精彩评论