gpt4 book ai didi

fluent-nhibernate - Fluent NHibernate - 覆盖表名

转载 作者:行者123 更新时间:2023-12-04 07:50:24 28 4
gpt4 key购买 nike

应用程序有许多扩展程序集,它们包含其类的映射。我需要为这些映射中的所有(基本、连接、多对多等)表名添加前缀。

例如

Assembly:        ~/bin/Extensions/Foo.dll
Original table: Page
New table: Ext_Foo_Page

Assembly: ~/bin/Extensions/Bar.dll
Original table: Page
New table: Ext_Bar_Page

最直接的方法是什么?

我试过了

public class TableNameConvention : IClassConvention, IJoinedSubclassConvention, IHasManyToManyConvention
{
private string getPrefix()
{
return "Ext_Test_";
}
public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
{
instance.Table(getPrefix() + instance.TableName);
}

public void Apply(FluentNHibernate.Conventions.Instances.IJoinedSubclassInstance instance)
{
instance.Table(getPrefix() + instance.TableName);
}
public void Apply(FluentNHibernate.Conventions.Instances.IManyToManyCollectionInstance instance)
{
instance.Table(getPrefix() + instance.TableName);
}
}

但即使执行了这些方法,它也不会更改表名。

编辑 - 配置

var sb = new StringBuilder();
var sw = new StringWriter(sb);

var cfg = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(b => b.Server(@".\SQLEXPRESS").Database("test123").Username("sa").Password("...")))
.Mappings(m => m.FluentMappings.AddFromAssembly(assembly).Conventions.Add<TableNameConvention>().ExportTo(sw))
.ExposeConfiguration(c => { new SchemaUpdate(c).Execute(false, true); })
.BuildSessionFactory();

var xml = sb.ToString();

最佳答案

使用 FNH 1.2.0.712

它适用于

var model = new PersistenceModel();
model.Add(typeof(EntityMap));
model.Conventions.Add<TableNameConvention>();
model.WriteMappingsTo(Console.Out);

但不是

m.FluentMappings.Add(typeof(EntityMap)).Conventions.Add<TableNameConvention>().ExportTo(Console.Out)

第二个例子没有调用,可能是bug。但以下作品

.Mappings(m => 
{
m.AutoMappings.Add(() => new AutoPersistenceModel().Conventions.Add<TableNameConvention>());
m.FluentMappings.Add(typeof(EntityMap)).ExportTo(Console.Out);
})

关于fluent-nhibernate - Fluent NHibernate - 覆盖表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7689358/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com