The contents of this section
Introduced
Example analysis
1. Tables and their constraints
2. Stored procedures, views
Conclusion
Introduced
In the last chapter we explored the use of the Schemaexport tool, knowing how to use the Schemaexport tools and schemaupdate tools to delete, create, and update database schemas using NHibernate persistence classes and mapping files. How does this specific analysis add some constraints to the table field? How do I build a stored procedure? How do I build a view? Use the Schemaexport tool to help you get it done.
Example Analysis 1. Table and its constraints
As we all know, the Schemaexport tool generates a database schema based on the mapping file, which makes it easy to generate database tables through class mapping in the mapping file. But this one we look at the mapping conditions, so I redefine two entities Categoryschema and Productschema, one-to-many relationships.
STEP1: Two entity persistence classes write code as follows:
public class CategorySchema
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
public class ProductSchema
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual int UnitsOnStock { get; set; }
public virtual CategorySchema CategorySchema { get; set; }
}
STEP2: Mapping for two entities, using the simplest way to write code as follows:
<?xml version="1.0" encoding="utf-8" ?>
assembly="DomainModel" namespace="DomainModel">
<class name="DomainModel.Entities.CategorySchema,DomainModel">
<id name="Id">
<generator class="guid"/>
</id>
<property name="Name"/>
</class>
<class name="DomainModel.Entities.ProductSchema,DomainModel">
<id name="Id">
<generator class="guid"/>
</id>
<property name="Name"/>
<many-to-one name="CategorySchema"
class="DomainModel.Entities.CategorySchema,DomainModel"/>
</class>