概要:
Fluent NHibernate Automapper を使用して、名前が同じで名前空間が異なる 2 つのクラスを保存したい
環境
テストのために、さまざまなオブジェクトをデータベースにインポートする必要があると書いています。最終的には、マッパーを適切なモデルに書き込みます。
コード生成と Fluent NHibernate を使用して、これらの DTO を取得し、直接 db にダンプしています。
例外は言う (auto-import="false" を使用してみてください)
コード
public class ClassConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Table(instance.EntityType.Namespace.Replace(".", "_"));
}
}
namespace Sample.Models.Test1
{
public class Test
{
public virtual int Id { get; set; }
public virtual string Something { get; set; }
}
}
namespace Sample.Models.Test2
{
public class Test
{
public virtual int Id { get; set; }
public virtual string SomethingElse { get; set; }
}
}
そして、これが実際のアプリコードです
var model = AutoMap.AssemblyOf<Service1>()
.Where(t => t.Namespace.StartsWith("Sample.Models"))
.Conventions.AddFromAssemblyOf<Service1>();
var cfg = Fluently.Configure()
.Database(
MySQLConfiguration.Standard.ConnectionString(
c => c.Is("database=test;server=localhost;user id=root;Password=;")))
.Mappings(m => m.AutoMappings.Add(model))
.BuildConfiguration();
new SchemaExport(cfg).Execute(false, true, false);
ありがとうございました
Fluent Nhibernate RC1 を使用した更新