NHibernate 3 specify sql data type with loquacious syntax

南楼画角 提交于 2019-12-04 04:43:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!