流利的NHibernate:如何在映射类中映射“外键”列 [英] Fluent NHibernate : How to map a 'Foreign Key' column in the mapping class
本文介绍了流利的NHibernate:如何在映射类中映射“外键”列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开始用Fluent NHiberate进行开发,我想知道如何在我的Mapping类中创建一个定义的外键关系。
这是我的课。这些类与关联的表是一对一的。
public class Song
{
public virtual int SongID {get;私人设置} //主键
public virtual int SongArtistID {get;组; } //外键映射到'Artist.ArtistID'
public virtual string Title {get;组; }
}
public class Artist
{
public virtual int ArtistID {get;私人设置} //主键
public virtual int ArtistName {get;组; }
}
public class SongMapping:ClassMap< Song>
{
SongMapping()
{
Id(c => c.SongID); //。GeneratedBy.HiLo(sermon); HiLo发电机好吗?
Map(c => c.SermonArtistID).Not.Nullable(); //这是如何映射到Artist.ArtistID?
Map(c => c.Title).Not.Nullable()。Length(50);
在我的映射文件中,我想创建一个定义外键关系在我的Song类SongArtistID列中,它将定义一个外键给Artist表/类中的ArtistID列。
public class Song
{
public virtual int Id {get;私人设置}
public virtual Artist Artist {get;组; }
public virtual string Title {get;组; }
public class SongMap:ClassMap< Song>
{
SongMap()
{
Id(c => c.Id);
参考文献(c => c.Artist); //是的,就是这样
Map(c => c.Title).Not.Nullable()。Length(50);
这就是说,使用 自动映射器配置 。 p>
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);
}
}
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.
解决方案 Here you go:
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);
}
}
}
That being said, it's easier using the Automapper configuration.
这篇关于流利的NHibernate:如何在映射类中映射“外键”列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文