duanshan5259 2009-08-16 07:03
浏览 45
已采纳

类似于Entity Framework的ORM NOT for .NET

What I really like about Entity framework is its drag and drop way of making up the whole model layer of your application. You select the tables, it joins them and you're done. If you update the database scheda, right click -> update and you're done again.

This seems to me miles ahead the competiting ORMs, like the mess of XML (n)Hibernate requires or the hard-to-update Django Models.

Without concentrating on the fact that maybe sometimes more control over the mapping process may be good, are there similar one-click (or one-command) solutions for other (mainly open source like python or php) programming languages or frameworks?

Thanks

  • 写回答

3条回答 默认 最新

  • doucheng8471 2009-08-16 12:13
    关注

    SQLAlchemy database reflection gets you half way there. You'll still have to declare your classes and relations between them. Actually you could easily autogenerate the classes too, but you'll still need to name the relations somehow so you might as well declare the classes manually.

    The code to setup your database would look something like this:

    from sqlalchemy import create_engine, MetaData
    from sqlalchemy.ext.declarative import declarative_base
    
    metadata = MetaData(create_engine(database_url), reflect=True)
    Base = declarative_base(metadata)    
    
    class Order(Base):
        __table__ = metadata.tables['orders']
    
    class OrderLine(Base):
        __table__ = metadata.tables['orderlines']
        order = relation(Order, backref='lines')
    

    In production code, you'd probably want to cache the reflected database metadata somehow. Like for instance pickle it to a file:

    from cPickle import dump, load
    import os
    
    if os.path.exists('metadata.cache'):
        metadata = load(open('metadata.cache'))
        metadata.bind = create_engine(database_url)
    else:
        metadata = MetaData(create_engine(database_url), reflect=True)
        dump(metadata, open('metadata.cache', 'w'))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建