问题
Is there anyway to add a prefix to table names at configuration time using Castle Active Record?
[ActiveRecord("Address")]
public class Address : ActiveRecord<Address> {}
I'd like the actual table created/referenced to be "PRODAddress" or "DEBUGAddress". Is there anything built-in like that I am not seeing?
Thank you,
[EDIT] I've marked the general answer below, but here is the actual code to implement table prefixes for Castle Active Record:
...
ActiveRecordStarter.ModelsCreated += ActiveRecordStarter_ModelsCreated;
ActiveRecordStarter.Initialize(source, typeof(Address));
...
private static void ActiveRecordStarter_ModelsCreated(ActiveRecordModelCollection models, IConfigurationSource source)
{
string tablePrefix = ConfigurationManager.AppSettings["TABLE_PREFIX"];
if (String.IsNullOrEmpty(tablePrefix)) return;
foreach (ActiveRecordModel model in models)
{
model.ActiveRecordAtt.Table = String.Format("{0}{1}", tablePrefix, model.ActiveRecordAtt.Table);
}
}
回答1:
You can use ActiveRecordStarter.RegisterExtension(IModelBuilderExtension extension) or the ActiveRecordStarter.ModelCreated event
回答2:
I think you'll have to configure your own INamingStrategy
来源:https://stackoverflow.com/questions/588360/table-prefix-using-castle-active-record