gpt4 book ai didi

nhibernate - 流利的 NHibernate : How to map a 'Foreign Key' column in the mapping class

转载 作者:行者123 更新时间:2023-12-05 08:24:32 26 4
gpt4 key购买 nike

我开始使用 Fluent NHiberate 进行开发,我想知道如何在我的映射类中创建定义的“外键”关系。

这是我的类(class)。这些类与相关表是一对一的。

public class Song
{
public virtual int SongID{ get; private set; } //Primary Key
public virtual int SongArtistID { get; set; } //Foreign Key mapping to 'Artist.ArtistID'
public virtual string Title { get; set; }
}

public class Artist
{
public virtual int ArtistID{ get; private set; } //Primary Key
public virtual int ArtistName{ get; set; }
}

public class SongMapping : ClassMap<Song>
{
SongMapping()
{
Id(c => c.SongID);//.GeneratedBy.HiLo("sermon"); is HiLo generator good?
Map(c => c.SermonArtistID).Not.Nullable(); //How is this mapped to 'Artist.ArtistID'??
Map(c => c.Title).Not.Nullable().Length(50);
}
}

在我的映射文件中,我想在我的 Song 类 SongArtistID 列中创建一个已定义的外键关系,这将为 Artist 表/类中的 ArtistID 列定义一个外键。

最佳答案

给你:

public class Song
{
public virtual int Id { get; private set; }
public virtual Artist Artist { get; set; }
public virtual string Title { get; set; }

public class SongMap : ClassMap<Song>
{
SongMap()
{
Id(c => c.Id);
References(c => c.Artist); // Yes, that's all.
Map(c => c.Title).Not.Nullable().Length(50);
}
}
}

也就是说,使用 Automapper configuration 更容易.

关于nhibernate - 流利的 NHibernate : How to map a 'Foreign Key' column in the mapping class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3691366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com