gpt4 book ai didi

c# - 具有 nvarchar(max) 的 Fluent NHibernate 自动映射字符串列表

转载 作者:行者123 更新时间:2023-11-30 22:34:34 24 4
gpt4 key购买 nike

我正在为 NHibernate 3.1 使用 Fluent NHibernate 1.2。我有一个类:

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

在 Marks 类的映射中,我有:

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

创建表后,将创建一个“答案”表,其中包含以下列:

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

我想做的是将值设置为 nvarchar(max)。我不希望对每个类中的每个字符串都执行此操作,而只对这一类执行此操作。

我看过这些帖子:first , second , third ,但还没有找到任何有帮助的东西。

在此先感谢您提供的任何帮助。如果您需要更多信息,请告诉我。

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

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

最佳答案

您可以通过使用 CustomSqlType("nvarchar(max)") 强制将 string 映射到映射中每个列级别的更长列,或者更普遍地,通过设置 Length(4001)(SQL Server 魔数(Magic Number),超过它会自动创建 nvarchar(max))。

要将它自动应用于实体中的所有 string 列,您可以编写自己的 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);
}
}

并将其注册到映射容器中,即像这样:

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

要将其应用于字符串集合,您可以使用自定义元素映射:

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

关于c# - 具有 nvarchar(max) 的 Fluent NHibernate 自动映射字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7771443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com