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条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容