我要表,表人和个人资料。个人资料的人物PK为FK。 我也有两个班:
public class Person
{
public int Id
{
get;set;
}
public Profile Profile
{
get;set;
}
}
public class Profile
{
Public int PersonId
{
get;set;
}
Public string Language
{
get;set;
}
}
我的映射是:
public class ProfileMap : ClassMap<Profile>
{
public ProfileSettingsMap()
{
Id(x => x.PersonId).GeneratedBy.Assigned();
Map(x => x.Language, "LanguageId");
}
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.Id).GeneratedBy.Identity();
HasOne(p => p.ProfileSettings).Cascade.All();
}
}
现在,在更新现有的Profile对象时,它工作正常,但在尝试插入新的配置文件时,我得到:
无法执行批处理命令。[SQL:SQL不可用]
PersonId是Profile对象是0(调试时)
我该如何解决这个问题?
提前致谢
答案 0 :(得分:0)
你告诉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();
}
}
或将配置文件映射为人员组件
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();
}
}
}