NHibernate (to. net) Tour: Preliminary discussion Schemaexport Tool use (this article is the same as hibernate) __.net

Source: Internet
Author: User
Tags generator object model
nhibernate Tour (19): A preliminary exploration of the use of Schemaexport tools2008-11-11 17:06 by Li Yongjing, 10660 visits, collection, edit

This section introduces the Schemaexport tool Schemaupdate Tool Instance analysis conclusion to introduce

I actually always write the persistence class and the mapping file first, and then use the Schemaexport tool to build the database schema. This approach is domain driven design/development (Ddd,domain driven design/development). My understanding is that the design of the system should be based on the object model, mainly consider the design and logic of the object, and then set up the database relational model according to the object model, which is now the process of object-oriented development, not the previous design of the database and then design objects. Use a picture to describe the field-driven design:

When designing, our domain model needs to be changed by simply modifying the nhibernate structure and application without having to modify the database schema, just by using the Schemaexport tool to regenerate the database schema. But using a database is just one way, and we can also use an XML file to store data. Schemaexport Tools

NHibernate's Hbm2dll provides schemaexport tools: Given a connection string and mapping file, you can automatically generate the database schema according to the persistence class and mapping file without having to input anything else, and now the Schemaexport tool is not very powerful, But the general application is enough, it is still a fairly primitive API is still improving.

The Schemaexport tool is to output DDL scripts to standard output while/or to execute DDL statements. The Schemaexport tool provides three methods, namely drop (), Create (), execute (), and the first two methods invoke the Execute () method in essence. You typically use the Execute () method to build the database schema. schemaupdate Tools

The new Schemaupdate tool is added to the NHibernate2.0 to update the database schema. But I don't think it works because it doesn't drop existing tables or columns, or update existing columns, only add new tables and columns. If I need to delete a table or column or modify one of the columns, the Schemaupdate tool seems powerless. Example Analysis

Know the above knowledge on a good combat: see how the specific use of it. 1.SchemaExport Tool Combat

Usually we use the build database schema code instance like this:

Configuration cfg=new Configuration ();
Cfg. Configure ();
Schemaexport Export =new schemaexport (CFG);
Export. Execute (...);

1. Preparatory work

Now the data access test layer creates a new SchemaExportFixture.cs file for test generation combat. Declare a global variable _cfg, and write a [SetUp] method that is invoked before each test method executes:

[Testfixture]
public class Schemaexportfixture
{
    private Configuration _cfg;
    [SetUp]
    public void Setupcontext ()
    {
        _cfg = new Configuration ();
        _cfg. Configure ();
    }
    Test ...
}

2. Test drop (script, export) method

[Test]
public void Droptest ()
{
    var export = new Schemaexport (_cfg);
    Export. Drop (True, true);

The drop (script, export) method performs a delete database schema based on the persistence class and mapping file. There are two parameters, the first true is to output the DDL statement to the console, the second true is to perform the delete database schema operation based on the persistence class and the mapping file, and debugging to discover that the drop (script, export) method essentially executes execute ( Script, export, True, true) method.

3. Test Create (script, export) method

[Test]
public void Createtest ()
{
    var export = new Schemaexport (_cfg);
    Export. Create (True, true);
}

The Create (Script,export) method creates a delete database schema after the schema is deleted based on the persistence class and mapping file. There are two parameters, the first is true is to output the DDL statement to the console, the second is true is based on the persistence class and mapping file to perform the deletion before the creation operation, after debugging can be found that the essence of this method is execute execute (script,export, False, True) method.

4. Test execute (script, export, Justdrop, format) method

[Test]
public void Executetest ()
{
    var export = new Schemaexport (_cfg);
    Export. Execute (True, True, false, false);

The Execute (script, export, Justdrop, format) method creates a delete database schema after the schema is deleted based on the persistence class and mapping file. There are four parameters, the first true is to output the DDL statement to the console; the second true is to perform the deletion and then the creation operation in the database based on the persistence class and mapping file, and the third is false to not only execute the DROP statement but also perform the creation operation. The difference in this argument extends the above two methods, and the fourth argument to false indicates that the output DDL statement is not formatted to the console and is output on one line.

The so-called formatted output is like this:

One line of output is like this:

5. Test execute (script, export, Justdrop, format, connection, Exportoutput) method

[Test]
public void Executeouttest ()
{
    var export = new Schemaexport (_cfg);
    var sb = new StringBuilder ();
    TextWriter output = new StringWriter (SB);
    Export. Execute (True, False, False, false, NULL, output);
}

The Execute (script, export, Justdrop, format, connection, Exportoutput) method creates a delete database schema after the schema is deleted based on the persistence class and mapping file. There are six parameters, the first is true to output the DDL statement to the console, the second false is not executing the DDL statement, and the fifth is the custom connection. The connection must be opened when the export is true to execute the statement. This method does not close the connection, NULL is using the default connection, the last parameter custom output, here I output to the TextWriter. 2.SchemaUpdate Tool Combat

Now the data access test layer creates a new SchemaUpdateFixture.cs file for test generation combat. First declare a global variable _cfg:

Private Configuration _cfg;

Here I configure the mapping file in a different way, first defining the two mapping XML representing the old and the new so that the meaning of the test update database schema can be reflected.

Old mapping xml: Here I use the product persistence class, because before we defined the product persistence class, we directly simulate the definition mapping XML: Owning the primary key ProductID and the Name field.

Public Const string product_xml =
    "<?xml version= ' 1.0 ' encoding= ' utf-8 '?> '" +
    "
Public Const string newproduct_xml =
    "<?xml version= ' 1.0 ' encoding= ' utf-8 '?> '" +
    "

Create a database schema using the old mapping XML before testing: use [SetUp] to execute before testing, create a database schema and format output DDL statements according to the old map xml:

[SetUp]
public void Setupcontext ()
{
    //simulate old system
    _cfg = new Configuration ();
    _cfg. Configure ();
    _cfg. Addxml (product_xml);
    var export = new Schemaexport (_cfg);
    Export. Execute (True, True, false, true);

Test Update Database Schema: The unique execute (script, doupdate) method provided using the Schemaupdate class updates the database schema according to the new mapping XML:

[Test]
public void Updateexistingdatabaseschematest ()
{
    _cfg = new Configuration ();
    _cfg. Configure (). Addxml (newproduct_xml);
    var update = new Schemaupdate (_cfg);
    Update. Execute (True, true);
}

Test the output as shown in the figure, if you feel uneasy and look at the database product table.

Did you see it? This is obviously not what I asked for, I first created the database schema according to the old mapping XML, but the update database schema seems powerless, adding only the Cost field, I want to update the Name field property is not nullable and the length is 50, but the Schemaupdate tool does not. I don't think this class has any effect at the moment, look forward to the next version to improve. Conclusion

This article introduces two utilities in NHibernate, using the persistence class and mapping file to generate the database schema in the Schemaexport tool. The Schemaupdate tool updates the database schema by persisting classes and mapping files.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.