Overloads for DataContext.CreateDatabase & DataContext.DeleteDatabase - Write DDL to TextWriter

תשובה Overloads for DataContext.CreateDatabase & DataContext.DeleteDatabase - Write DDL to TextWriter

  • Friday, December 07, 2007 7:43 PM
     
     
    It would be nice if in a future version we could be able to generate SQL from the entities, like Castle Active Record supports:

    using(StreamWriter sw = new StreamWriter("create_database.sql"))
    {
    dataCtxt.CreateDatabase(sw);
    }


All Replies

  • Monday, January 07, 2008 7:25 PM
     
     Answered
    You can get all the DDL to a text writer right now actually.  Just set the datacontext's Log property before you call CreateDatabase.  I've just tried this now.  You still have to actually create the database but, as it is created, all the DDL gets logged to the TextWriter which you have attached to the Log property.

    John
  • Monday, January 07, 2008 7:35 PM
     
     

    I've not used Castle Active Record myself, however I have used Rails - is this similar to the migrations?  What is inside the SQL Script create database?

     

    If you create a DataContext in your system (using SQLMetal or the Designer), Linq can already create the database for you by calling the DataContext.CreateDatabase() method.

     

    The DataContext can also execute standard SQL by going DataContext.ExecuteCommand(sql).

     

    Regards

     

    Ben

  • Monday, January 07, 2008 11:51 PM
     
     
    All I'm talking about is logging the SQL that is issued by the CreateDatabase method which you mention.  It is a full script to create the database from scratch.  I believe that's not the same as Rail's migrations.
  • Monday, January 07, 2008 11:54 PM
     
     

    Oh, you can log all the queries using the DataContext.Log property.  You can then out it to a file to write out everything.

     

    I talked about how to get it to write everything to the console on my blog.

    http://blog.benhall.me.uk/2007/05/tracing-linq-to-sql-queries-without.html

     

    Hope this helps.

     

     

    Ben

     

  • Tuesday, January 08, 2008 8:59 AM