Fluent nHibernate automapping property as nvarchar(max)

安稳与你 提交于 2019-12-22 03:24:36

问题


using fluent nhibernate, and automappings (nhibernate creates my db schema), how can i get nhibernate to create a nvarchar(max) column in the database based on the following class

public class VirtualPage : BaseEntity
{
    public virtual int ParentId { get; set; }
    public virtual string PageName { get; set; }
    public virtual string Title { get; set; }
    public virtual string Body { get; set; }
    public virtual string ViewName { get; set; }
    public virtual string ViewData { get; set; } // this must be nvarchar(max)
}

回答1:


With automapping you can override the default length for text fields, but it will be applied to all text fields.

You should be able to combine automapping with explicit mappings created with the fluent API.

Fortunately, this is a pretty simple class to map (I'm assuming that this is part of a table-per-subclass hierarchy, which is why I use SubClassMap<> instead of ClassMap<> and do not map an identifier):

public class VirtualPageMap : SubClassMap<VirtualPage>
{
    public VirtualPageMap()
    {
        Map(x => x.ParentId);
        Map(x => x.PageName);
        Map(x => x.Title);
        Map(x => x.Body);
        Map(x => x.ViewName);
        Map(x => x.ViewData).Length(4001); // anything over 4000 is nvarchar(max)
    }
}

I've actually never used automappings, so I'm assuming that this will be picked up properly, but do not know for sure.

Don't forget to add the mapping in your configuration.

Fluently.configure(
    // blah blah blah
    .Mappings(m => 
    {
        m.FluentMappings.AddFromAssemblyOf<VirtualPage>();
        m.AutoMappings.Add( // blah blah blah
    }



回答2:


Set the Length property to a high number (I use 10000) - this will cause NHibernate to make a nvarchar(max)



来源:https://stackoverflow.com/questions/3532298/fluent-nhibernate-automapping-property-as-nvarcharmax

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