[英]Fluent NHibernate DuplicateMappingException with AutoMapping
摘要:
我想用Fluent NHibernate Automapper保存兩個相同名稱和不同名稱空間的類
上下文
我寫的是必須將許多不同的對象導入數據庫進行測試。 我最終會將mappers寫成一個合適的模型。
我一直在使用code gen和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進行更新
James Gregory 撰寫的關於fluent-nhibernate論壇的解決方案
今晚好好看看這個。 基本上,它是由AutoImport提到的異常; 當NHibernate給出第一個映射時,它看到實體以完整的程序集限定名稱命名,並為短名稱創建導入(有用!),然后當你添加第二個時它然后抱怨這個導入現在發生沖突。 所以解決方案是關閉自動導入; 不幸的是,我們沒有辦法在RC中做到這一點...我剛剛提交了一個修復程序,增加了在約定中更改此功能的能力。 因此,如果您獲得最新的二進制文件或源代碼,您應該能夠更改附加項目中的約定行以執行此操作:
.Conventions.Setup(x => {
x.AddFromAssemblyOf<Program>();
x.Add(AutoImport.Never()); });
這會添加您在程序集中定義的所有約定,然后使用其中一個幫助程序約定來關閉自動導入。
我無法使用FoursMappings的約定(與AutoMappings相比)使其工作。 但是,以下內容適用於我,但必須將其添加到每個需要的ClassMap中。
public class AMap : ClassMap<A>
{
public AMap()
{
HibernateMapping.Not.AutoImport();
Map(x => x.Item, "item");
...
}
}
使用BeforeBindMapping事件可以訪問.HBM XML文件的對象表示形式。
此事件允許您在創建NHibernate會話工廠之前在運行時修改任何屬性。 這也使得FluentNHibernate等效約定變得不必要。 不幸的是,目前還沒有關於這個非常棒的功能的官方文檔。
這是復制映射問題的全局解決方案(請記住,所有HQL查詢現在都需要使用完全限定類型名稱而不僅僅是類名稱)。
var configuration = new NHibernate.Cfg.Configuration();
configuration.BeforeBindMapping += (sender, args) => args.Mapping.autoimport = false;
我不得不在哪里添加AutoImport.Never()的約定。 我將持久性映射分成不同的項目 - 每個應用程序的模型也可以在不同的項目中找到。 使用它與Fluent NHibernate和自動映射。
在某些情況下,域名,井映射確實必須結合起來。 這將是我需要訪問所有域的時候。 使用的POCO類有時會具有相同的名稱和不同的名稱空間,就像上面的例子一樣。
這是我的組合所有映射的樣子:
internal static class NHIbernateUtility
{
public static ISessionFactory CreateSessionFactory(string connectionString)
{
return Fluently.Configure()
.Database(
MsSqlConfiguration
.MsSql2008
.ConnectionString(connectionString))
.Mappings(m => m.AutoMappings
.Add(ProjectA.NHibernate.PersistenceMapper.CreatePersistenceModel()))
.Mappings(m => m.AutoMappings
.Add(ProjectB.NHibernate.PersistenceMapper.CreatePersistenceModel()))
.Mappings(m => m.AutoMappings
.Add(ProjectC.NHibernate.PersistenceMapper.CreatePersistenceModel())).BuildSessionFactory();
}
}
還有一個持久性映射器:
public static class PersistenceMapper
{
public static AutoPersistenceModel CreatePersistenceModel()
{
return
AutoMap.AssemblyOf<Credential>(new AutoMapConfiguration())
.IgnoreBase<BaseEntity>()
.Conventions.Add(AutoImport.Never())
.Conventions.Add<TableNameConvention>()
.Conventions.Add<StandardForeignKeyConvention>()
.Conventions.Add<CascadeAllConvention>()
.Conventions.Add<StandardManyToManyTableNameConvention>()
.Conventions.Add<PropertyConvention>();
}
}
持久性映射器對於每個POCO命名空間非常相似 - 有些具有覆蓋。 我不得不將.Conventions.Add(AutoImport.Never())添加到每個持久性映射器,它就像一個魅力。
只是想分享這個,如果其他人這樣做的話。
我對此有真正的問題,上面的示例或其任何變體都無濟於事。
var cfg = new NotifyFluentNhibernateConfiguration();
return Fluently.Configure()
.Database(
FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005
.ConnectionString("Server=10.2.65.227\\SOSDBSERVER;Database=NotifyTest;User ID=NHibernateTester;Password=test;Trusted_Connection=False;")
)
.Mappings(m => {
m.AutoMappings
.Add(AutoMap.AssemblyOf<SubscriptionManagerRP>(cfg));
m.FluentMappings.Conventions.Setup(x =>
{
x.AddFromAssemblyOf<Program>();
x.Add(AutoImport.Never());
});
} )
.BuildSessionFactory();
我找不到程序的參考..
我也試圖在絕望中放下一個單獨的xml文件。配置流暢的nhibernate映射到auto-import = false但沒有成功。
我可以請一些關於如何做到這一點的更廣泛的例子嗎?
編輯,我在幾周前得到了最新的主干。
編輯,通過刪除所有重復項解決此問題。
我曾經也有過一樣的問題。 我這樣解決了:
Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(...)
.AdoNetBatchSize(500))
.Mappings(m => m.FluentMappings
.Conventions.Setup(x => x.Add(AutoImport.Never()))
.AddFromAssembly(...)
.AddFromAssembly(...)
.AddFromAssembly(...)
.AddFromAssembly(...))
;
導入的部分是: .Conventions.Setup(x => x.Add(AutoImport.Never()))
。 這個配置似乎一切正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.