繁体   English   中英

流利的Nhibernate映射hasMany

[英]Fluent Nhibernate mapping hasMany

在我的MSSQL中,我有两个表,Property和Photo。

为了缩短它,我将在这里写几个字段。 属性表

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

关系如下:

FK_Property_Photo

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

你可以想象一个属性可以有一个或多个图像。 一个图像可以属于一个或多个属性。

我尝试过这种映射

public PropertyMap()
{
  Table("Property");
  Id(x => x.Id).GeneratedBy.Identity();
  Map(x => x.Title).Length(255).Not.Nullable();
  HasMany(x => x.Photos).KeyColumn("Id");
}

public PhotoMap()
 {
    Table("Photo");
    Id(x => x.Id).GeneratedBy.Identity();
    Map(x => x.Version);
    Map(x => x.ImageData).CustomSqlType("VARBINARY(MAX)").Length(160000);
    Map(x => x.ImageMimeType);
 }

您想要使用References和HasMany关联。 您已经在使用HasMany,因此要获得其他关联:

public PropertyMap()
{
  Table("Property");
  Id(x => x.Id).GeneratedBy.Identity();
  Map(x => x.Title).Length(255).Not.Nullable();
  HasMany(x => x.Photos).KeyColumn("Id"); // you were already doing this
}

public PhotoMap()
 {
    Table("Photo");
    Id(x => x.Id).GeneratedBy.Identity();
    Map(x => x.Version);
    Map(x => x.ImageData).CustomSqlType("VARBINARY(MAX)").Length(160000);
    Map(x => x.ImageMimeType);
    References( x => x.Property ) // you'll need 'Property' in your class definition too
        .Column('PhotoId')
        .Cascade.All();
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM