nhibernate会议

时间:2012-03-20 07:40:30

标签: asp.net-mvc nhibernate

实体照片属于Property实体,一个属性可以有很多照片。映射很好,创建数据也没问题。在db中,例如存储我的照片 Id 1 binarydata PropertyId(100) ID为100的物业文章参考了许多照片。我写这些都是为了说我的数据创建是好的并且也是映射。

因此,问题在于在显示属性详细信息时加载照片集。 我需要在会话中加载照片集,所以我被困在这里。

 public ActionResult Details(int id)
        {
            MyDomain.Property data = null;
            using (//session)
            {
                using (//transaction)
                {
                    data = session.QueryOver<MyDomain.Property>()
                        .Where(x => x.Id == id)
                        .Fetch(x => x.Photos).Eager //empty
                        .Fetch(x => x.Features).Eager
                        .SingleOrDefault<MyDomain.Property>();

                  //I was thinking here to call 
                  // data.Photos = GetMyPhotos(id);
                    tx.Commit();
                    return PartialView("_HomePageDetailsPartial", data);
                }

            }
            //return PartialView("_HomePageDetailsPartial", data);        
        }

正如你在我看到的那样,我尝试了data.Photos = GetMyPhotos(id);但在调试时我有错误消息无法更新标识列'Id'。
无法更新标识列'Id'。 即使是这项工作,我也相信有一些更优雅的方法来检索特定属性的照片集。

我的映射

public class PhotoMap : ClassMap<Photo>
    {
        public PhotoMap()
        {
            Table("Photo");
            Id(x => x.Id).GeneratedBy.Identity();
            Map(x => x.ImageData).CustomSqlType("VARBINARY(MAX)").Length(160000).Not.Nullable();
            Map(x => x.ImageMimeType).Not.Nullable();
            References(x => x.Property).Column("PropertyId");

        }
    }

public class PropertyMap : ClassMap<Property>
    {
        public PropertyMap()
        {
            Table("Property");
            Id(x => x.Id).GeneratedBy.Identity();
            ...
            References(x => x.Features, "FeaturesId");
            HasMany(x => x.Photos).KeyColumn("Id").Cascade.All();
        }
    }

数据库快照 两张桌子,物业和照片。

Id int not null
Title nvarchar(255) not null
PhotoId int not null

照片桌

Id int not null
ImageData varbinary(MAX) null
ImageMimeType varchar(50) null
PropertyId int not null

关系如下:

FK_Property_Photo

Primary Key table        Foreign key table
--------------------------------------------
Photo                    Property
--------------------------------------------
Id                       PhotoId

1 个答案:

答案 0 :(得分:4)

您的映射中的KeyColumn是错误的。 KeyColumn用于定义n表中的外键列。在您的情况下,键列应为“PropertyId”。

另外:如果属性和照片之间的关系是1:n,为什么你的属性表中有一个PhotoId列?