我无法计算出两个相关实体(Asset和AssetType)的代码映射。一个资产只有一个AssetType。AssetType有许多资产。我想要资产的所有权、容量、费用以及资产类型中的类型。以下是实体。Asset.cs
public class Asset
{
public virtual int ID { get; set; }
public virtual string Title { get; set; }
public virtual decimal Fee { get; set; }
public virtual int Capacity { get; set; }
public virtual int AssetTypeID { get; set; }
public virtual AssetType AssetType { get; set; }
}
public class AssetMap : ClassMapping<Asset>
{
public AssetMap()
{
Table("Asset");
Id(x => x.ID, x => x.Generator(Generators.Identity));
Property(x => x.Title, x => x.NotNullable(true));
Property(x => x.Fee, x => x.NotNullable(true));
Property(x => x.Capacity, x => x.NotNullable(true));
Property(x => x.AssetTypeID, x => x.NotNullable(true));
Bag(t => t.AssetType, t =>
{
t.Table("AssetType");
t.Key(k => k.Column("ID"));
}, t => t.OneToMany(k => k.Column("AssetTypeID")));
}
}
AssetType.cs
public class AssetType
{
public virtual int ID { get; set; }
public virtual string Type { get; set; }
public virtual IList<Asset> Assets { get; set; }
}
public class AssetTypeMap : ClassMapping<AssetType>
{
public AssetTypeMap()
{
//AssetType AssetType = null;
Table("AssetType");
Id(x => x.ID, x => x.Generator(Generators.Identity));
Property(x => x.Type, x => x.NotNullable(true));
ManyToOne(x => x.Assets, map => {
map.Column("ID"); map.Cascade(Cascade.All);
});
}
}
这是错误消息:
错误1方法的类型参数'NHibernate.MMapping.ByCode.Impl.CustomizersImpl.PropertyContainerCustomizer.Bag(字符串,System.Action>,System.Action>)'无法根据用法推断。尝试指定类型参数明确地C: \Users\Aatish\Documents\Visual Studio2013\Projects\EventPlanner\Solutions\EventPlanner \EventPlanner\Models\Asset.cs 32 13 EventPlanner
感谢
是否可能缺少一列或多列上的类型定义?你能分享你试图在给定异常中运行的查询结果吗?
作者共享的异常可能是指定类型丢失或错误的结果
指定类型的映射示例
public class AssetTypeMap : ClassMapping<AssetType>
{
public AssetTypeMap()
{
...
Table("AssetType");
...
Property(x => x.Type, x => {x.NotNullable(true); x.Type(NHibernateUtil.String);});
...
}
}