本文介绍了Fluent NHibernate:如何在映射类中映射“外键"列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开始使用 Fluent NHiberate 进行开发,我想知道如何在我的 Mapping 类中创建定义的外键"关系.
I'm starting to develop with Fluent NHiberate, and I was wondering how I create a defined 'Foreign Key' relationship in my Mapping class.
这是我的课.这些类与关联表是一对一的.
Here's my class. These classes are a one-to-one with associated tables.
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 列定义一个外键.
In my mapping file, I want to create a defined foreign key relationship in my Song class SongArtistID column, which will define a foreign key to the ArtistID column in the Artist table/class.
推荐答案
给你:
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 配置更容易.
That being said, it's easier using the Automapper configuration.
这篇关于Fluent NHibernate:如何在映射类中映射“外键"列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!