O / R who can automatically update the database schema?

Are there any O / R cards that will automatically create or modify the database schema when updating business objects? Looking around, it seems that most libraries work in a different way, creating a business object from a database schema.

The reason I want to be able to do this is because I am planning a product that stores its data in a database on a client’s machine. Therefore, I may need to update the database schema when a new version is released.

Another requirement is that mapper supports a file database such as SQLite or JET, not just a SQL server.

I know that Developer Express XPO has this capability, but I was wondering if there were any alternatives.

thanks

+3
source share
3 answers

SubSonic does what you need. It has a migration class for use with sonic commander. I changed the code and put it at the beginning of my applications, and it checks the difference in version and updates automatically. Here's how you need to configure it:

  • You need to add a migration class with version. You could take a look at this tutorial if you have not used SubSonic before. The next class is saved as \ Migrations \ 001_initial.cs, if you called migrate.exe (inside VS) from subsonic, it will update the current db to the latest available version. You need to save the three-digit version prefix as the file name for migrate.exe for proper operation, as indicated in the tutorial. As for the class name, the migration class I wrote requires an underscore and a three-digit version prefix for proper version detection.

// * I don’t know why I need this line to make the code block below

namespace MyApps.Migrations { public class _001_Initial : Migration { public override void Up() { //Execute your upgrade query here } public override void Down() { //Execute your downgrade query here } } } 
  1. Called by CheckForMigration () at launch of your application. IsUpdateAvailable will indicate that you need to update db. You just need to call Migrate (). IsAppVersionOlder indicates that your application was created with an older version of the db schema (perhaps another copy of the new applications updated db). With the help of, you can prevent the launch of old applications and damage the updated db.

//

 using System; using System.Collections.Generic; using SubSonic; using SubSonic.Migrations; namespace MyApps.Migrations { internal static class MigrationHelper { const string NameSpace = "MyApps.Migrations"; private const string SCHEMA_INFO = "SubSonicSchemaInfo"; public static int CurrentVersion { get { return currentVersion; } } public static int AppVersion { get { return latestVersion; } } public static bool IsUpdateAvailable { get { return (updateVersion.Count > 0); } } public static bool IsAppVersionOlder { get; private set; } public static bool Checked { get; internal set; } private static int currentVersion; private static int latestVersion; private static List<string> updateVersion; private static List<string> availableVersion; static MigrationHelper() { Checked = false; } /// <summary> /// Migrates the specified migration directory. /// </summary> public static void CheckForMigration() { currentVersion = Migrator.GetCurrentVersion("YourProviderName"); Type[] allTypes = System.Reflection.Assembly.GetExecutingAssembly().GetTypes(); availableVersion = new List<string>(); foreach (Type type in allTypes) { if (type.Namespace == NameSpace) if (type.Name.Substring(0, 1) == "_") availableVersion.Add(type.Name); } availableVersion.Sort(); updateVersion = new List<string>(); foreach (string s in availableVersion) { int version = 0; if (int.TryParse(s.Substring(1,3), out version)) { if (version > currentVersion) { updateVersion.Add(s); } latestVersion = version; } } IsAppVersionOlder = (latestVersion < currentVersion); //log.WriteLine(string.Format( ///"CheckForMigration: DbVer = {0}, AppVer = {1}, UpdateAvailable = {2}, IsAppOlder = {3}", //currentVersion, latestVersion, updateVersion.Count, IsAppVersionOlder)); Checked = true; } internal static void Migrate() { foreach (string s in updateVersion) { Migration _migration = (Migration)Activator.CreateInstance( System.Reflection.Assembly.GetExecutingAssembly().GetType( "MyApps.Migrations." + s)); _migration.Migrate("YourProviderName", Migration.MigrationDirection.Up); IncrementVersion(); } } private static void IncrementVersion() { new Update(SCHEMA_INFO, "YourProviderName").SetExpression("version").EqualTo("version+1").Execute(); } } } 

A table called SubSonicSchemaInfo will be automatically added by SubSonic to your db to track the version of db.

This is a long post, I hope I haven’t missed anything.

+1
source

NHibernate can generate a database schema from business objects for you.

+2
source

Can NHibernate refresh the circuit or does it fall and recreate it?

0
source

All Articles