I have to convert a set of C# classes (class library) to SQL tables to be used by SQL Server, so that the data can be stored in a database and manipulated through the database.
The problem is that the number of these classes is big (more than 1000 classes), and it would take a long time to setup manually such a database schema (tables, indexes, stored procedures, etc.) - not to mention the class hierarchies that I need to maintain.
So, the question is:
Is there a tool that would help me to create a database schema from a C# class library?
I am not seeking for the perfect tool (but if there is such a tool I would be very happy to know), but for a tool that will help me at least create a starting point.
Please, also note that I would prefer your suggested solution to refer to version 2.0 of the .NET Framework, and being based on traditional practices (T-SQL code like: CREATE TABLE ...
), although every suggestion is welcomed.
If the class structure is relational and relatively simple to convert to SQL Create Table or and EF model perhaps you can write your own code generation script to create the .edmx or SQL.
You can use T4 or CodeDom and it would probably take a lot less time than manually creating 1000 tables.
EDIT: I forgot about this when I first wrote my answer. A while back I saw a screencast by Rob Conery about Subsonic Simple Repositroy. Simple Repository allows you to insert an object into the DB and if there is no table for it it creates one for you.
Check out the video here http://subsonicproject.com/docs/Using_SimpleRepository
I don't know of any tool, but for that many tables its perhaps worth writing a small tool using Reflection to get all the properties and spit out create table statement with column name and type defined. A perfect script will be hard to achieved, but like stated in your question, this can give you a very good starting point to then tweak the field type and size.
Just to expand a bit on what others are saying--most ORM-type tools have model-first capability. Much like EF4, NHibernate, and Telerik Open Access, Subsonic (http://www.subsonicproject.com/) has the simple repository pattern which can generate tables from classes.
An alternate option here, is to write a simple reflection-based tool to traverse your classes.
Although you didn't ask, I'm curious about your data access needs. We hire relational databases for three primary tasks: persistence, efficient retrieval of information, and declarative referential integrity (e.g. foreign keys). Just creating a table per class is only half the battle.
Do you have a high volume of data (i.e. 100's of gigs or multiple-terabytes)? Lots of reads? lots of writes? Do the classes have natural primary keys? Surrogate keys? Do your classes define relations between each other (i.e.
Employees
work inFactories
).Do classes have lots of fields? Do you have any meta data that indicates data types with precision, i.e. not just
string Name
but thatName
can be a maximum of 80 characters and is not nullable? Do you need to store English only or do you use languages that require extended character sets like Mandarin? If you don't have a method to indicate precision your database will end up having seriously wide rows (or potentially wide). Many databases limit maximum row size to something in the 8k-64k range. Wide rows impact read & write performance. Usingtext
fields may get around the page size limits, but will result in much more expensive read performance.Just as important as implementing the database structure is indexing it and using referential integrity. Do the number of classes/tables grow over time? Maybe a more abstract structure with fewer tables would be appropriate.
Just my 2c. Hope that helps.
There's a new CTP out of "Code First" Entity Framework. You may be able to use that to generate your database directly from code after a bit more code writing.
It'll still take a while with 1000 tables to create.
Most object-relational mapping tools include an option to create a database schema based on your mapped classes.
Here's a complete example of how you'd do this using NHibernate and Fluent NHibernate. This is a standalone C# console app that'll map a collection of C# business objects and create a database schema based on them.
Two important caveats:
Here's program.cs. Note the three namespaces - one containing the program itself, one containing the entities, and one containing an example of a mapping override for Fluent NHibernate (useful if your objects don't adhere to the built-in mapping conventions)
And here's what's exported to schema.sql by the above code:
If you could use Visual Studio 2010 v4.0 framework there are some new capabilities to generate scripts using "Model First" and entity framework. This isn't helpful for v2.0 obviously but I thought it was worth a mention.
The "Model First" feature lets you design a conceptual (CSDL) model in the Entity Designer, and then generate the storage (SSDL) model, as well as the mapping (MSL) between these. In addition, we also generate T-SQL scripts to create the database for you. To run the Model First wizard, just right-click on the Entity Designer surface, and choose "Generate Database from Model...". There's an in-depth blog post here. Also, check this article out.