开发者

NHibernate - How do I change schemas during run time?

开发者 https://www.devze.com 2022-12-10 06:54 出处:网络
I\'m using NHibernate to connect to an ERP database on our DB2 server. We have a test schema and a production schema. Both schemas have the same table structure underneath. For testing, I would like t

I'm using NHibernate to connect to an ERP database on our DB2 server. We have a test schema and a production schema. Both schemas have the same table structure underneath. For testing, I would like to use the same mapping classes but point NHibernate to the test environment when needed and then b开发者_StackOverflow中文版ack when in production. Please keep in mind that we have many production schemas and each production schema has an equivalent test schema.

I know that my XML mapping file has a schema property inside it, but since it's in XML, it's not like I can change it via a compiler directive or change the schema property based on a config file.

Any ideas?

Thank You.


No need to specify schema in the mappings: there's a SessionFactory-level setting called default_schema. However, you can't change it at runtime, as NHibernate pregenerates and/or caches SQL queries, including the schema part.


To get what I wanted, I had to use NHibernate.Mapping.Attributes.

[NHibernate.Mapping.Attributes.Class(0, Table = “MyTable”, Schema = MySchemaConfiguration.MySchema)]

In this way, I can create a class like MySchemaConfiguration and have a property inside of it like MySchema. I can either set the property's value via a compiler directive or get it through a configuration file. This way I only have to change the schema in one place and it will be reflected throughout all of the other mappings.


I have found following link that actually fixes the problem.

How to set database schema for namespace in nhibernate

The sample code could be

cfg.ClassMappings.Where(cm => cm.Table.Schema == "SchemaName")
    .ForEach(cm => cm.Table.Schema = "AnotherSchemaName");

This should happen before you initialize your own data service class.

@Brian, I tried NHibernate.Mapping.Attributes, the attribute value you put inside should be a constant. So it could not be updated during run time. How could you have set the property's value using a parameter value in configuration file?


The code to fix HBM XML resources.

    // This is how you get all the hbm resource names.
    private static IList<string> GetAllHbmXmlResourceNames(Assembly assembly)
    {
        var result = new List<string>();

        foreach (var resource in assembly.GetManifestResourceNames())
        {
            if (resource.EndsWith(".hbm.xml"))
            {
                result.Add(resource);
            }
        }

        return result;
    }
    // This is how you get the stream for each resource.
    Assembly.Load(assembly).GetManifestResourceStream(name)
    // What you need to do next is to fix schema name in this stream
    // Replacing schema name.
    private Stream FixSchemaNameInStream(Stream stream)
    {
        StreamReader strStream = new StreamReader(stream);
        string strCfg = strStream.ReadToEnd();
        strCfg = strCfg.Replace(string.Format("schema=\"{0}\"" , originalSchemaName), string.Format("schema=\"{0}\"" , newSchemaName));

        return new MemoryStream(Encoding.ASCII.GetBytes(strCfg));
    }


Take a look at SchemaUpdate.

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/28/create-and-update-database-schema.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消