同为nvarchar字符串的功能NHibernate自动映射列表(最大值) [英] Fluent NHibernate automap list of strings with nvarchar(max)

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

问题描述

我使用功能NHibernate 1.2 NHibernate的3.1。我有一个类:

 公共类商标
{
公共虚拟INT标识{搞定;组; }
公共虚拟的IList<串GT;答案{搞定;组; }
}

在为商标类的映射,我有:



 的hasMany(M = GT; m.Answers).Element(值); 

在创建表,一个答案表出现以下列创建:

  Marks_id(FK,INT NOT NULL)
值(为nvarchar(255),NULL)

我想这样做是有价值为nvarchar(最大)。我不想这样做,对于每一个类每串,只为这一个班



我已经看过这些帖子:的first ,的第二,的第三,但没有发现任何事情没有什么帮助。



预先感谢您可以提供任何帮助。如果您需要更多的信息,请让我知道



编辑:
这是解决问题的代码:

 的hasMany(X => x.Answers).Element(值,X => x.Columns.Single() 。长度= 4001); 


解决方案

您可以强制映射字符串在映射每列级通过使用 CustomSqlType(为nvarchar(最大))或更长的柱,有点普遍,通过设置长度(4001)(SQL Server的幻数,超过这个它创建为nvarchar(最大)自动)。



要在您的实体自动为它所有的字符串列,您可以编写自己的 FluentNHibernate约定

 公共类LongStringConvention:IPropertyConvention ,IPropertyConventionAcceptance 
{
公共无效接受(IAcceptanceCriteria< IPropertyInspector>的标准)
{
criteria.Expect(X => x.Type == typeof运算(字符串));
}

公共无效申请(IPropertyInstance实例)
{
instance.Length(4001);
}
}

和IE中一样,映射容器注册它:

  Fluently.Configure()
.Mappings(...)
.Conventions.Add< LongStringConvention> ()

要应用它的字符串的集合,你可以使用自定义元素映射:

 的hasMany(X => x.Answers).Element(值,X =方式> x.Columns.Single()长度= 4001); 


I am using Fluent NHibernate 1.2 for NHibernate 3.1. I have a class:

public class Marks
{
    public virtual int Id { get; set; }
    public virtual IList<string> Answers { get; set; }
}

In the mapping for the Marks class, I have:

HasMany(m => m.Answers).Element("Value");

When the tables are created, an "Answers" table get created with the following columns:

Marks_id (FK, int, not null)
Value (nvarchar(255), null)

What I would like to do is have the Value be nvarchar(max). I'd prefer not doing this for every string in every class, just for this one class.

I have looked at these posts: first, second, third, but haven't found anything yet that helps.

Thanks in advance for any help you can offer. If you need additional information, please let me know.

Edit: This is the code that resolves the issue:

HasMany(x => x.Answers).Element("Value", x => x.Columns.Single().Length = 4001);

解决方案

You can force mapping string to longer column at each column level in mapping either by using CustomSqlType("nvarchar(max)") or, bit more universally, by setting Length(4001) (SQL Server magic number, above which it creates nvarchar(max) automatically).

To apply it automatically for all string columns in your entities, you can write your own FluentNHibernate convention:

public class LongStringConvention : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Type == typeof(string));
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.Length(4001);
    }
}

And register it in mapping container i.e. like that:

Fluently.Configure()
    .Mappings(...)
    .Conventions.Add<LongStringConvention>()

To apply it for collection of strings, you can use custom element mappings:

HasMany(x => x.Answers).Element("Value", x => x.Columns.Single().Length = 4001);

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

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