1

There are a lot of Sql table -> C# class methodologies, but I'm looking for the reverse.

Hypothetical situation:

I have N classes populated by some web service I consume, manipulate, then preform an action on. Now the boss wants said web service data persisted in a database.

I already have the classes defined, how can I quickly and easily (aka, lazily) generate a sql table off of each class?

chad
  • 717
  • 1
  • 9
  • 23
  • Possible duplicate: http://stackoverflow.com/questions/47239/how-can-i-generate-database-tables-from-c-classes – Paolo Tedesco Oct 05 '11 at 19:53
  • Easier said than done. How would you store STRING data in the database? VARCHAR() -How do you know the size to use. NVARCHAR() - Does the string need Unicode characers. CHAR()? Should booleans be stored as TinyInt. Should Private members be stored? What about GET/SET properties that convert a data type such as a flag into a literal value. But, it would be a handy tool – Sparky Oct 05 '11 at 19:54

3 Answers3

3

Entity Framework and nHibernate both allow you to use them in "code-first" mode, which involves writing classes then generating databases.

There is a walk-through of this on ScottGu's blog here: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

Dylan Smith
  • 22,069
  • 2
  • 47
  • 62
1

you can use MS Entity Framework Code First approach:

Code First allows you to define your model using C# or VB.Net classes, optionally additional configuration can be performed using attributes on your classes and properties or by using a Fluent API. Your model can be used to generate a database schema or to map to an existing database.

read more about it here: EF 4.1 Code First Walkthrough

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
0

Entity framework 4 has a code first development feature, I haven't used it so can't really recommend it

ScottGu blog

Pharabus
  • 6,081
  • 1
  • 26
  • 39