问题
I'm trying to map an Entity with a string property to a varchar column in NHibernate 3 using the new Loquacious API but I can't figure out how to specify the Type to use. I am able to correctly map the entity with NHibernate 2 and FluentNHibernate.
NHibernate 2 w/Fluent Mapping
public class EntityMapping : ClassMap<Entity>
{
public EntityMapping()
{
Table("EntityTable");
Id(x => x.EntityId).Column("EntityId").GeneratedBy.Identity();
Map(x=>x.Code).Not.Nullable().Column("EntityCode").CustomType("AnsiString");
}
}
NHibernate 3 w/loquacious API
public Action<IClassMapper<Entity>> CreateMapping()
{
return ca =>
{
ca.Table("Entity");
ca.Id(x => x.EntityId, map =>
{
map.Column("EntityId");
map.Generator(Generators.Identity);
});
ca.Property(x => x.Code, map =>
{
map.Column(cm => {
cm.Name("EnityCode");
cm.NotNullable(true);
});
});
};
How/where do I specify "AnsiString" (so queries against code are parameterized as 'varchar' instead of 'nvarchar' when the SQL is constructed)?
I am using Sql Server 2008.
回答1:
ca.Property(x => x.Code, map =>
{
map.Type(NHibernateUtil.AnsiString);
map.Column(/*etc.*/);
});
回答2:
An answer to the question itself ("specify sql type") would be to define the property like this:
ca.Property(x => x.Code, map =>
{
map.Column(cm => {
cm.Name("EnityCode");
cm.NotNullable(true);
cm.SqlType("varchar");
});
});
Directly specifying the SQL type should be considered a hack which is not necessary here, though. It's preferable to use the map.Type(NHibernateUtil.AnsiString);
solution and have NHibernate deduce the SQL type.
来源:https://stackoverflow.com/questions/7420524/nhibernate-3-specify-sql-data-type-with-loquacious-syntax