流利的Nhibernate映射hasMany

Bob*_*ock 9 c# mapping nhibernate fluent-nhibernate

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

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

Id int not null
Title nvarchar(255) not null
PhotoId int not null
Run Code Online (Sandbox Code Playgroud)

照片桌

Id int not null
ImageData varbinary(MAX) null
ImageMimeType varchar(50) null
Run Code Online (Sandbox Code Playgroud)

关系如下:

FK_Property_Photo

Primary Key table        Foreign key table
--------------------------------------------
Photo                    Property
--------------------------------------------
Id                       PhotoId
Run Code Online (Sandbox Code Playgroud)

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

我尝试过这种映射

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);
 }
Run Code Online (Sandbox Code Playgroud)

dwe*_*ner 13

您想要使用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();
 }
Run Code Online (Sandbox Code Playgroud)