Setting string to be sql type of “varchar” instead of “nvarchar”

前端 未结 4 2099
醉酒成梦
醉酒成梦 2020-12-18 22:10

I have the following mapping:

public class LogEntryMap
{
    public LogEntryMap()
    {
        Map.Id(x => x.Id).GeneratedBy.Identity();
        Map(x =&         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-18 22:38

    If you wanted all of your strings to be mapped to varchar instead of nvarchar you could consider using a convention:

    /// 
    /// Ensures that all of our strings are stored as varchar instead of nvarchar.
    /// 
    public class OurStringPropertyConvention : IPropertyConvention
    {
        public void Apply(IPropertyInstance instance)
        {
            if (instance.Property.PropertyType == typeof (string))
                instance.CustomType("AnsiString");
        }
    }
    

    You mappings could then go back to a simple mapping:

    Map(x => x.Context);
    

    Just make sure you remember to tell Fluent NH to use the convention:

            var configuration = new Configuration();
            configuration.Configure();
            Fluently
                .Configure(configuration)
                .Mappings(m => m.FluentMappings
                    .AddFromAssemblyOf()
                    .Conventions.Add()
                    )
                .BuildSessionFactory();
    

提交回复
热议问题