代码映射NHibernate

时间:2015-12-22 15:59:32

标签: c# nhibernate

我无法弄清楚2个相关实体(Asset和AssetType)的代码映射。资产只有一个AssetType。 AssetType有许多资产。我希望从Asset中获得Title,Capacity,Fee和AssetType中的Type。以下是实体。 的 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.Mapping.ByCode.Impl.CustomizersImpl.PropertyContainerCustomizer.Bag(字符串,   System.Action&gt;中   System.Action&GT)”   无法从使用中推断出来。尝试指定类型参数   明确。 C:\ Users \ Aatish \ Documents \ Visual Studio   2013 \ Projects \ EventPlanner \ Solutions \ EventPlanner \ EventPlanner \ Models \ Asset.cs 32 13 EventPlanner

谢谢

1 个答案:

答案 0 :(得分:0)

您是否可能缺少一列或多列上的类型定义?您可以共享您尝试运行的查询,这会导致给定的异常吗?

作者共享的异常可能是指定类型缺失或错误的结果

指定类型的示例映射

public class AssetTypeMap : ClassMapping<AssetType>
    {
        public AssetTypeMap()
        {
            ...
            Table("AssetType");
            ...
            Property(x => x.Type, x => {x.NotNullable(true); x.Type(NHibernateUtil.String);});
           ...
        }
    }