ActiveRecord's multi-database configuration basically inherits the NHibernate idea, but makes some adjustments on the configuration file structure. NHibernate configuration is also based on configuration, configuring multiple sessionfactory to pass in multiple base classes
1. Use the inheritance method to summarize the type of the same database. such as a, B, C, D, E in a, B connected to the database test1,c, D to Test2, and E to the default test, then the specific code will be the following way.
Public Abstract class test1base:activerecordbase{} Public Abstract class Test2base:activerecordbase{}[activerecord ("A")] Public class a:test1base{}
[ActiveRecord ("B")]
public class b:test1base{
}
[ActiveRecord ("C")]
public class c:test2base{
}
[ActiveRecord ("D")]
public class d:test2base{
}
[ActiveRecord ("E")]
public class e:activerecordbase{
}
Config.
<?xml version="1.0"encoding="Utf-8"?><activerecord> <config> <add key="Hibernate.connection.driver_class"Value="NHibernate.Driver.SqlClientDriver"/> <add key="Hibernate.dialect"Value="NHibernate.Dialect.MsSql2000Dialect"/> <add key="Hibernate.connection.provider"Value="NHibernate.Connection.DriverConnectionProvider"/> <add key="hibernate.connection.connection_string"Value="Data source=localhost;initial catalog=test; Uid=sa; Password=sa"/> </config> <config type="ConsoleApplication1.CastleActiveRecord.Test1Base, Learn.cui"> <add key="Hibernate.connection.driver_class"Value="NHibernate.Driver.SqlClientDriver"/> <add key="Hibernate.dialect"Value="NHibernate.Dialect.MsSql2000Dialect"/> <add key="Hibernate.connection.provider"Value="NHibernate.Connection.DriverConnectionProvider"/> <add key="hibernate.connection.connection_string"Value="Data source=localhost;initial Catalog=test1; Uid=sa; Password=sa"/> </config> <config type="ConsoleApplication1.CastleActiveRecord.Test2Base, Learn.cui"> <add key="Hibernate.connection.driver_class"Value="NHibernate.Driver.SqlClientDriver"/> <add key="Hibernate.dialect"Value="NHibernate.Dialect.MsSql2000Dialect"/> <add key="Hibernate.connection.provider"Value="NHibernate.Connection.DriverConnectionProvider"/> <add key="hibernate.connection.connection_string"Value="Data source=localhost;initial Catalog=test2; Uid=sa; Password=sa"/> </config></activerecord>
We will find that ActiveRecord implements multiple database connection configurations by using a common base class. As long as you inherit from the specified base class, we can use a different target database. In the configuration file we can let ActiveRecord use the multi-database configuration by adding multiple config sections and specifying the Config Type property. The config type is the base class we write, and all types that inherit from that base class will use that connection configuration, and those that inherit directly from Activerecordbase or use the default connection configuration.
The above example uses different databases for SQL Server 2000, and we can also connect to different database systems, such as A, B connected to SQL Server,c, D to DB2, and so on.
2. The base class needs to follow certain rules.
(1) must inherit from Activerecordbase.
(2) must be an abstract class.
(3) The [ActiveRecord ()] attribute can be not added.
(4) The abstract base class must be initialized. such as "activerecordstarter.initialize (source, typeof (Test1base));", if you use the Activerecordstarter.initialize ( Assembly.getexecutingassembly (), source); "Then add" [ActiveRecord ()] "to the abstract base class.
The following is a relatively complete code demonstration.
namespaceconsoleapplication1.castleactiverecord{[ActiveRecord ()] Public Abstract classbase:activerecordbase {} [ActiveRecord ("Users")] Public classUser:base {Private intID; [PrimaryKey (primarykeytype.identity)] Public intId {Get{Console.WriteLine ("Id ...");returnID;} Set{id =value;} } Private stringname; [Property (Unique=true)] Public stringName {Get{returnname;} Set{name =value;} } } Public classActiverecordtest {Staticactiverecordtest () {//Get database Connection ConfigurationXmlconfigurationsource Source =NewXmlconfigurationsource (@"Config/activerecord.xml"); //loads all ActiveRecord classes in the assembly. activerecordstarter.initialize (assembly.getexecutingassembly (), source); //self-loading of the specified type//activerecordstarter.initialize (source, typeof (Activerecordbase), typeof (User), typeof (Base)); //To Delete a database schema//Activerecordstarter.dropschema (); //Create a database schema (this method deletes a table with the same name before it is created)Activerecordstarter.createschema (); } Public Static voidTest () {User User=NewUser (); User. Name="Tom"+NewRandom (DateTime.Now.Millisecond). Next (); Activerecordmediator.create (user); }}}activerecord.xml<?xml version="1.0"encoding="Utf-8"?><activerecord> <config> <add key="Hibernate.connection.driver_class"Value="NHibernate.Driver.SqlClientDriver"/> <add key="Hibernate.dialect"Value="NHibernate.Dialect.MsSql2000Dialect"/> <add key="Hibernate.connection.provider"Value="NHibernate.Connection.DriverConnectionProvider"/> <add key="hibernate.connection.connection_string"Value="Data source=localhost;initial Catalog=test2; Uid=sa; Password=sa"/> </config> <config type="ConsoleApplication1.CastleActiveRecord.Base, Learn.cui"> <add key="Hibernate.connection.driver_class"Value="NHibernate.Driver.SqlClientDriver"/> <add key="Hibernate.dialect"Value="NHibernate.Dialect.MsSql2000Dialect"/> <add key="Hibernate.connection.provider"Value="NHibernate.Connection.DriverConnectionProvider"/> <add key="hibernate.connection.connection_string"Value="Data source=localhost;initial catalog=test; Uid=sa; Password=sa"/> </config></activerecord>
ActiveRecord Multi-database configuration