Auto create properties from database tables

Yakkity

Member
Joined
Oct 7, 2009
Messages
7
Programming Experience
1-3
Hi all,


I'm wondering if there's a way to autogenerate classes with properties + getters and setters from the columns in my database table. I'm using one table which has over 50 columns, another one over 70 ... about 20 tables with 20 columns ... and ofcourse I don't want to manually do this.


Thanks in advance,
- Yakkity
 
Last edited:
Hmmm don't think it's either of those 2 options.

My project consists out of 3 layers, presentation, business logic and data acces.
I can succesfully interact between these layers and do operations on my database from my presentation layer.

Now I want to add a sub layer ( property layer ) in my busines logic like they explain here: Three Tier Architecture in ASP.NET . So I want to "map" all my tables and their columns into classes.

For example let's say I have a table "Persons" with columns "lastName" and "firstName". Now I'd like to auto-generate a class called Persons with properties lastName and firstName AND their getters and setters.

Again, thanks in advance
-Yakkity
 
As JohnH says, this will be generated for you by creating and using a typed dataset (.xsd).
 
There are various things you can do. You can create a typed DataSet as suggested, or you can use LINQ to SQL or the Entity Framework to generate data entity classes for you. You can also use a third-party tool line LLBLGen Pro, NHibernate or SubSonic. Finally, you could write your own tool to read a database schema and generate class definitions from templates.
 
Well, I'm working with the 3 layer principle, data access, business logic & presentation layer. Which would you advise me to use with this principle ?
 
Well, I'm working with the 3 layer principle, data access, business logic & presentation layer. Which would you advise me to use with this principle ?
Any and all can be used in n-tier systems.
 
So with the typed dataset.

Say I have a database with table Person and 2 String properties firstName and lastName.

I create a dataset with dataTable called Person and I then use an instance of PersonRow, just like I'd normally use a self-made class Person with properties firstName and lastName + getters & setters.

Correct ?
 
That's correct. With an untyped DataSet it would look something like this:
VB.NET:
Dim table As DataTable = myDataSet.Tables("Person")
Dim row As DataRow = table.NewRow()

row("FirstName") = "John"
row("LastName") = "Smith"
table.Rows.Add(row)
This is from memory so it may not be quite right but with a typed DataSet it would look something like this:
VB.NET:
Dim table As PersonDataTable = myDataSet.Person
Dim row As PersonDataRow = table.NewPersonDataRow()

row.FirstName = "John"
row.LastName = "Smith"
table.AddPersonDataRow(row)
Note that you're using typed properties instead of strings to identify tables and columns, so you get full design-time Intellisense and type-checking.
 
Back
Top