功能NHibernate自动映射财产为nvarchar(最大) [英] Fluent nHibernate automapping property as nvarchar(max)

查看:126
本文介绍了功能NHibernate自动映射财产为nvarchar(最大)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用功能NHibernate和automappings(NHibernate的创建我的DB模式),我怎样才能NHibernate的,以在数据库中创建一个为nvarchar(max)列基于以下类



 公共类VirtualPage:BaseEntity 
{
公共虚拟INT的ParentId {搞定;组; }
公共虚拟字符串的页面名称{搞定;组; }
公共虚拟字符串名称{搞定;组; }
公共虚拟绳体{搞定;组; }
公共虚拟字符串VIEWNAME {搞定;组; }
公共虚拟字符串的ViewData {搞定;组; } //这一定是为nvarchar(最大)
}


解决方案

使用自动映射您可以覆盖文本字段的默认长度,但它会被应用到所有文本字段。



您应该能够自动映射具有明确结合用流利的API创建的映射。



幸运的是,这是一个非常简单的类映射(我假定这是一个表,每个子类层次结构的一部分,这就是为什么我用 SubClassMap<> 而不是 ClassMap<> 并没有映射的标识符):

 公共类VirtualPageMap:SubClassMap< VirtualPage> 
{
公共VirtualPageMap()
{
地图(X => x.ParentId);
地图(X => x.PageName);
地图(X => x.Title);
地图(X => x.Body);
地图(X => x.ViewName);
地图(X => x.ViewData)。长度(4001); //任何超过4000类型为nvarchar(最大)
}
}



我'实际上已经没有用过automappings,所以我假定这将适当回升,但不知道是肯定的。



不要忘了添加映射在您的配置。

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


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)
}

解决方案

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
    }

这篇关于功能NHibernate自动映射财产为nvarchar(最大)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆