public class PersonEntity
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Email { get; set; }
public virtual string Phone { get; set; }
public virtual string MobilePhone { get; set; }
}
public class PersonMap : ClassMapping<PersonEntity>
{
public PersonMap()
{
Schema("dbo");
Table("People");
Id(x => x.Id, map => map.Generator(Generators.Identity));
Property(x => x.FirstName, map => map.NotNullable(true));
Property(x => x.LastName, map => map.NotNullable(true));
Property(x => x.Email, map => map.NotNullable(true));
Property(x => x.Phone, map => map.NotNullable(true));
Property(x => x.MobilePhone);
}
}
両方のクラスが同じアセンブリにあります
public ISessionFactory CreateSF()
{
FluentConfiguration fCfg = Fluently.Configure().
Database( MsSqlConfiguration.MsSql2008.ConnectionString( ConfigurationManager.ConnectionStrings["eDb"].ConnectionString).ShowSql());
fCfg.Mappings(m => m.FluentMappings.AddFromAssemblyOf<PersonMap>());
return fCfg.BuildSessionFactory();
}
と私のユニットテスト
var sf = CreateSF();
using (var _session = sf.OpenSession())
{
new PersistenceSpecification<PersonEntity>(_session)
.CheckProperty(x => x.Id, 1)
.CheckProperty(x => x.FirstName, "FName")
.CheckProperty(x => x.LastName, "LName")
.CheckProperty(x => x.MobilePhone, "12345")
.CheckProperty(x => x.Phone, "987654")
.CheckProperty(x => x.Email, "[email protected]")
.VerifyTheMappings();
}
例外をスローしています
NHibernate.MappingException:永続化なし:Ers.Data.PersonEntity a
Nhibernate3.3.3を使用しています。FluentNHibernate 1.4.0.0
それを修正する別の方法はありますか?私はすでに流暢なマッピングを指定しています。
私は他に何かが恋しいですか?
これは本当に興味深い問題です。いいね…それで、あなたは何を知っていますか?
mapping-by-code
fluent-nhibernate
ここで比較と違いを確認してくださいNHibernateのコードによるマッピング
言い換えれば、流暢なマッピングを使用するだけで、構成はそれを見つけるでしょう...それだけです
流暢なマッピングの例
public class PersonMap : ClassMap<PersonEntity>
{
public PersonMap()
{
Schema("dbo");
Table("People");
Id(x => x.Id).GeneratedBy.Identity();
Property(x => x.FirstName).Not.Nullable();
Property(x => x.LastName).Not.Nullable();
Property(x => x.Email).Not.Nullable();
Property(x => x.Phone).Not.Nullable();
Property(x => x.MobilePhone);
}
本当にとても素敵なリンクがありますが、mapping-by-code
正直なところ、これらは流暢なマッピングの最良の情報源です (常に記事の下部にあります)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加