您告诉 NH,您正在自己维护一对一 ( GeneratedBy.Assigned()
)
要么将其映射为onetoone
public class ProfileMap : ClassMap<Profile>
{
public ProfileMap()
{
Id(x => x.PersonId).GeneratedBy.Foreign("Person"); // assuming you have a reference to Person in Profile
Map(x => x.Language, "LanguageId");
}
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.Id).GeneratedBy.Identity();
HasOne(p => p.ProfileSettings).Cascade.All();
}
}
或将配置文件映射为 Person 的组件
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.Id).GeneratedBy.Identity();
Join("Profiles", join =>
{
join.Component(p => p.ProfileSettings, c =>
{
c.Map(x => x.LanguageId);
});
}
}
}
更新:控制台应用程序中的此代码适用于 FNH 1.2
public class Person
{
public virtual int Id { get; set; }
public virtual Profile Profile { get; set; }
}
public class Profile
{
public virtual int RecordsPerPage { get; set; }
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.Id).GeneratedBy.Identity();
Join("Profile", join =>
{
join.Component(p => p.Profile, c =>
{
c.Map(x => x.RecordsPerPage, "RecordsPerPage");
});
});
}
}
static void Main(string[] args)
{
var config = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.Mappings(m => m.FluentMappings.Add<PersonMap>())
.BuildConfiguration();
using (var sf = config.BuildSessionFactory())
using (var session = sf.OpenSession())
{
new SchemaExport(config).Execute(true, true, false, session.Connection, null);
using (var tx = session.BeginTransaction())
{
session.Save(new Person { Profile = new Profile { RecordsPerPage = 5 } });
tx.Commit();
}
}
}