2009-12-21 155 views
2

我有一个实体,像这样:如何使用Fluent NHibernate自动映射映射字典?

public class Land 
{ 
    public virtual IDictionary<string, int> Damages { get; set; } 
    // and other properties 
} 

每次我尝试使用自动映射用下面的代码:

var sessionFactory = Fluently.Configure() 
    .Database(SQLiteConfiguration.Standard.InMemory) 
    .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Land>)) 
    .BuildSessionFactory(); 

我收到以下错误:

{"The type or method has 2 generic parameter(s), but 1 generic argument(s) were 
provided. A generic argument must be provided for each generic parameter."} 

人告诉我我做错了什么?另外,这只是一个简单的例子。我有更多的词典比这个更多。

+0

http://stackoverflow.com/questions/1410716/fluentnhibernate-mapping-for-dictionary 尝试AsMap() – 2012-09-14 16:38:23

回答

9

NHibernate是不可能的。

+0

你是否认为这对于Fluent NHibernate automapping,Fluent NHibernate作为一个整体(也就是流利映射的意义)或NHibernate本身是不可能的? – 2010-01-01 02:05:13

+0

NHibernate本身。我不知道任何可以自动映射字典的ORM。 – user224564 2010-01-03 06:06:28

+8

你是对的,你必须手动映射它。使用Fluent,它将是'References(x => x.Dictionary).AsMap (“keyColumn”)。Element(“valueColumn”,c => c.Type ());'。 – 2010-01-11 05:18:09

3

发现一些痕迹,这isn't possible。一些痕迹,即it's recently implemented

仍在调查中。 :)


This looks quite promising(尚未测试)。

所以,你的情况应该像=>

public class LandMap : ClassMap<Land> 
{ 
    public LandMap() 
    { 
     (...) 

     HasMany(x => x.Damages) 
      .WithTableName("Damages") 
      .KeyColumnNames.Add("LandId") 
      .Cascade.All() 
      .AsMap<string>(
       index => index.WithColumn("DamageType").WithType<string>(), 
       element => element.WithColumn("Amount").WithType<int>() 
      ); 
    } 
} 

请记住 - 这应该。我没有测试它。

+0

这对流利的映射。我正在寻找一些适用于自动映射的功能,因为我的所有实体中都有大约50个字典。 – 2009-12-23 23:15:29

+0

啊......对不起。不知何故,没有注意到'automapping'。我会看一看。 :) – 2009-12-23 23:18:53

1

可能的解决方法应该与自动映射理论工作:

public class DamagesDictionary : Dictionary<string, int> 
{ 
} 

Land.cs

public class Land 
{ 
    public virtual DamagesDictionary Damages { get; set; } 
    // and other properties 
} 

或更通用的方法......

public class StringKeyedDictionary<T> : Dictionary<string, T> 
{ 
} 

Land.cs

public class Land 
{ 
    public virtual StringKeyedDictionary<int> Damages { get; set; } 
    // and other properties 
} 
+0

我认为这是一个被低估的答案 - 有时更简单的做另一个有2个属性的POCO(Key,Value),并且提到如果你不能编写映射(Lazy,yes .. Recommender,no ..但是...) – Darbio 2011-11-30 05:01:16

相关问题