代码之家  ›  专栏  ›  技术社区  ›  x__dos

使用NHibernate生成MySql模式

  •  0
  • x__dos  · 技术社区  · 17 年前

    我使用了以下代码来生成数据库创建脚本

    Configuration configuration = new Configuration();
    configuration.Configure();
    SchemaExport schemaExport = new SchemaExport(configuration);
    using(TextWriter stringWriter = new StreamWriter("create.sql"))
    {
       schemaExport.Execute(false, false, false, true, null, stringWriter); 
    }
    

    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
      <property name="connection.connection_string">Server=localhost;Database=db;User ID=root;Password=sa;</property>
      <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
    </session-factory>
    

    但由于错误,这次生成的sql不会在MySQL查询浏览器中执行。 以下是Nhibernate生成的mysql创建脚本示例:

    alter table Order  drop foreign key FK_User_Order
    alter table OrderItem  drop foreign key FK_Order_Item
    alter table Item  drop foreign key FK742DC178AD99756A
    
    drop table if exists User
    drop table if exists Order
    drop table if exists OrderItem
    drop table if exists Item
    drop table if exists Category
    create table User (
      Id INTEGER NOT NULL AUTO_INCREMENT,
       role INTEGER not null,
       is_active TINYINT(1) not null,
       contact_name VARCHAR(100),
       phone VARCHAR(100),
       address VARCHAR(100),
       email VARCHAR(100) not null,
       name VARCHAR(100) not null,
       password_hash LONGBLOB not null,
       login VARCHAR(100) not null unique,
       is_new TINYINT(1) not null,
       price_type INTEGER not null,
       access_id INTEGER not null,
       primary key (Id) 
    

    我在尝试执行它时出现以下错误

    脚本行:2您在中有一个错误 您的SQL语法;检查手册 对应于您的MySQL服务器 要使用的正确语法的版本 “近”顺序删除外键 FK_用户_订单

    alter table OrderItem drop foreign k' 在1号线

    你知道为什么Nhiberante生成的sql不能工作吗?

    1 回复  |  直到 17 年前
        1
  •  3
  •   Simon Simon    17 年前

    schemaExport.SetDelimiter(“;”);如果需要分隔(即将它们作为单独的语句运行)

    还有,订单是一个关键词吗?能否将订单重新映射到数据库中的订单,以避免关键字冲突?