In my MSSQL I have two tables, Property and Photo.
To make it shorter I will write here just few fields. Property table
Id int not null
Title nvarchar(255) not null
PhotoId int not null
Photo table
Id int not null
ImageData varbinary(MAX) null
ImageMimeType varchar(50) null
Relationship is as follows:
FK_Property_Photo
Primary Key table Foreign key table
--------------------------------------------
Photo Property
--------------------------------------------
Id PhotoId
As you can imagine one property can have one or many images. One image can belong to one or meny properties.
I Tried with this kind of mapping
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);
}
You want to make use of References and HasMany associations. You are already using HasMany, so to get the other association:
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();
}