I have some old code which didn't use database. Now can database schema be auto-generated from existing classes? I know this seems reverse than usual DB to class generation.
我有一些旧代码,没有使用数据库。现在可以从现有类自动生成数据库模式吗?我知道这似乎与通常的DB到类生成相反。
But it would be good if there is some easy way to do so.
但如果有一些简单的方法可以做到这一点会很好。
3 个解决方案
#1
2
Sure, with a little reflection you could generate some rough table creation scripts, and even stored procedures to support INSERTS/UPDATES/DELETES (if you wished).
当然,通过一点反射,您可以生成一些粗略的表创建脚本,甚至存储过程来支持INSERTS / UPDATES / DELETES(如果您愿意)。
Unless there is an exact one-to-one mapping, you will need to clean up those scripts by hand.
除非存在精确的一对一映射,否则您需要手动清理这些脚本。
EDIT: to elaborate on this further (in conjunction with @TomTom's answer)
编辑:进一步详细说明(结合@ TomTom的回答)
Things that will be difficult/impossible to infer automatically:
难以/不可能自动推断的事情:
- default column values
- 默认列值
- computed columns (if any)
- 计算列(如果有)
- varchar sizes
- varchar大小
- NULL/NOT NULL columns
- NULL / NOT NULL列
- indexes
- 索引
Things that you could do automatically with a bunch of work (but better to do by hand, IMO):
你可以通过一堆工作自动完成的事情(但最好是手工完成,IMO):
- primary key assignment
- 主键分配
- foreign key relationships
- 外键关系
- relationships
- 关系
#2
2
It is not. Or, only ins a very stupid way. Without additional annotations .NET contains a lot less information than a db schema. Conditions, checks arem issing. Data types are missing, too. And how you think such code would deduct what indices to have? Heck, even finding out the primary key of a class / table is not possible.
不是这样。或者,只是一种非常愚蠢的方式。如果没有其他注释,.NET包含的信息比数据库模式少得多。条件,检查正在发布。数据类型也缺失了。你认为这样的代码会如何扣除哪些指数?哎呀,即使找到一个类/表的主键也是不可能的。
Inheritance is another topic.
继承是另一个话题。
You can generate a schema as a start, but you can not really generate a database that is production ready without annotating the classes heavily.
您可以生成一个模式作为开始,但是如果不对类进行大量注释,则无法生成生产就绪的数据库。
#3
1
If you are using VS2010, you may want to look at Code-First development using Entity Framework, with your classes inheriting from DbContext. ScottGu has a good blog entry here on how to do that.
如果您使用的是VS2010,您可能希望使用Entity Framework查看Code-First开发,您的类继承自DbContext。 ScottGu在这里有一篇关于如何做到这一点的博客文章。
#1
2
Sure, with a little reflection you could generate some rough table creation scripts, and even stored procedures to support INSERTS/UPDATES/DELETES (if you wished).
当然,通过一点反射,您可以生成一些粗略的表创建脚本,甚至存储过程来支持INSERTS / UPDATES / DELETES(如果您愿意)。
Unless there is an exact one-to-one mapping, you will need to clean up those scripts by hand.
除非存在精确的一对一映射,否则您需要手动清理这些脚本。
EDIT: to elaborate on this further (in conjunction with @TomTom's answer)
编辑:进一步详细说明(结合@ TomTom的回答)
Things that will be difficult/impossible to infer automatically:
难以/不可能自动推断的事情:
- default column values
- 默认列值
- computed columns (if any)
- 计算列(如果有)
- varchar sizes
- varchar大小
- NULL/NOT NULL columns
- NULL / NOT NULL列
- indexes
- 索引
Things that you could do automatically with a bunch of work (but better to do by hand, IMO):
你可以通过一堆工作自动完成的事情(但最好是手工完成,IMO):
- primary key assignment
- 主键分配
- foreign key relationships
- 外键关系
- relationships
- 关系
#2
2
It is not. Or, only ins a very stupid way. Without additional annotations .NET contains a lot less information than a db schema. Conditions, checks arem issing. Data types are missing, too. And how you think such code would deduct what indices to have? Heck, even finding out the primary key of a class / table is not possible.
不是这样。或者,只是一种非常愚蠢的方式。如果没有其他注释,.NET包含的信息比数据库模式少得多。条件,检查正在发布。数据类型也缺失了。你认为这样的代码会如何扣除哪些指数?哎呀,即使找到一个类/表的主键也是不可能的。
Inheritance is another topic.
继承是另一个话题。
You can generate a schema as a start, but you can not really generate a database that is production ready without annotating the classes heavily.
您可以生成一个模式作为开始,但是如果不对类进行大量注释,则无法生成生产就绪的数据库。
#3
1
If you are using VS2010, you may want to look at Code-First development using Entity Framework, with your classes inheriting from DbContext. ScottGu has a good blog entry here on how to do that.
如果您使用的是VS2010,您可能希望使用Entity Framework查看Code-First开发,您的类继承自DbContext。 ScottGu在这里有一篇关于如何做到这一点的博客文章。