【问题标题】:Fluent Nhibernate mapping hasManyFluent Nhibernate 映射 hasMany
【发布时间】:2012-04-03 09:44:53
【问题描述】:

在我的 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);
 }

【问题讨论】:

  • 您希望关联出现在哪一边?我会看一下流利的文档,我自己更喜欢 xml 映射:S.
  • 如何将多张照片与一个属性相关联?有没有你没有提到的“链接”表?

标签: c# nhibernate fluent-nhibernate mapping


【解决方案1】:

您想使用References and 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();
 }

【讨论】:

  • 不客气。现在,当我看到它们时,我可能只会理解流利的映射。 :)
猜你喜欢
  • 2012-10-19
  • 1970-01-01
  • 2013-09-01
  • 2011-04-10
  • 2013-04-24
  • 1970-01-01
  • 2011-02-27
  • 2011-08-07
  • 2012-07-26
相关资源
最近更新 更多