C# Fluent Nhibernate 映射有很多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9761154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:45:14  来源:igfitidea点击:

Fluent Nhibernate mapping hasMany

c#nhibernatefluent-nhibernatemapping

提问by BobRock

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);
 }

采纳答案by dwerner

You want to make use of References and HasManyassociations. You are already using HasMany, so to get the other association:

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