Reputation: 349
Are there any O/R mappers out there that will automatically create or modify the database schema when you update the business objects? After looking around it seems that most libraries work the other way by creating business object from the database schema.
The reason I'd like to have that capability is that I am planning a product that stores its data in a database on the customer's machine. So I may have to update the database schema when a new version comes out.
Another requirement is that the mapper supports a file based database like SQLite or JET, not only SQL server.
I know XPO from Developer Express has that capability but I was wondering if there are any alternatives out there.
Thanks
Upvotes: 3
Views: 242
Reputation: 8347
SubSonic does what you need. It has a migration class to be use with sonic commander. I modified the code and put it in the startup of my apps, and have it check for version difference and upgrade automatically. This is how you need to setup it up:
//*Not sure why I need this line to make the code block working 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
}
}
}
//
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 named SubSonicSchemaInfo will be added automatically by SubSonic to your db to help keep track of the db version.
It's a long post, hope I didn't missed anything
Upvotes: 1
Reputation: 349
Can NHibernate update the schema or does it drop and recreate it?
Upvotes: 0
Reputation: 103135
NHibernate can generate the database schema from the business objects for you.
Upvotes: 2