问题
I have an entity like so:
public class Land
{
public virtual IDictionary<string, int> Damages { get; set; }
// and other properties
}
Every time I try to use automapping with the following code:
var sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory)
.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Land>))
.BuildSessionFactory();
I get the following error:
{"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."}
Can someone tell me what I'm doing wrong? Also, this is just a simple example. I have much more dictionaries than just this one.
回答1:
It is impossible with NHibernate.
回答2:
Found some traces that this isn't possible. Some traces, that it's recently implemented.
Still investigating. :)
This looks quite promising (didn't test yet).
So, in your case it should look like=>
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>()
);
}
}
Keep in mind - it should. I didn't test it.
回答3:
A possible workaround that should in theory work with automapping:
public class DamagesDictionary : Dictionary<string, int>
{
}
Land.cs
public class Land
{
public virtual DamagesDictionary Damages { get; set; }
// and other properties
}
or a more generic approach...
public class StringKeyedDictionary<T> : Dictionary<string, T>
{
}
Land.cs
public class Land
{
public virtual StringKeyedDictionary<int> Damages { get; set; }
// and other properties
}
来源:https://stackoverflow.com/questions/1943071/how-do-i-map-a-dictionary-using-fluent-nhibernate-automapping