简体   繁体   English

流利的Nhibernate映射hasMany

[英]Fluent Nhibernate mapping hasMany

In my MSSQL I have two tables, Property and Photo. 在我的MSSQL中,我有两个表,Property和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 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. 您想要使用References和HasMany关联。 You are already using HasMany, so to get the other association: 您已经在使用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