问题
I have a class CaptionItem
public class CaptionItem
{
public virtual int SystemId { get; set; }
public virtual int Version { get; set; }
protected internal virtual IDictionary<string, string> CaptionValues {get; private set;}
}
I am using following code for nHibernate mapping
Id(x => x.SystemId);
Version(x => x.Version);
Cache.ReadWrite().IncludeAll();
HasMany(x => x.CaptionValues)
.KeyColumn("CaptionItem_Id")
.AsMap<string>(idx => idx.Column("CaptionSet_Name"), elem => elem.Column("Text"))
.Not.LazyLoad()
.Cascade.Delete()
.Table("CaptionValue")
.Cache.ReadWrite().IncludeAll();
So in database two tables get created. One CaptionValue and other CaptionItem. In CaptionItem table has three columns
1. CaptionItem_Id int
2. Text nvarchar(255)
3. CaptionSet_Name nvarchar(255)
Now, my question is how can I make the columnt type of Text to nvarchar(max)?
Thanks in advance.
回答1:
I tried many things, but nothing was solving the problem. Eventually I solved it. I just changed a bit in the mapping code. The new mapping code is given below
Id(x => x.SystemId);
Version(x => x.Version);
Cache.ReadWrite().IncludeAll();
HasMany(x => x.CaptionValues)
.KeyColumn("CaptionItem_Id")
.AsMap<string>(idx => idx.Column("CaptionSet_Name"), elem => elem.Columns.Add("Text", c => c.Length(10000)))
.Not.LazyLoad()
.Cascade.Delete()
.Table("CaptionValue")
.Cache.ReadWrite().IncludeAll();
So I just added c => c.Length(10000). Now, in the database the columntype of Text will be nvarchar(MAX).
Hope it will help anybody else.
回答2:
You can do that, using the following:
Id(x => x.Text).CustomSqlType("nvarchar(max)");
// For older versions of Fluent NHibernate
Id(x => x.Text).CustomSqlTypeIs("nvarchar(max)");
Edit:
Bipul, Why not create a mapping for the CaptionValues and in that mapping, specify that, the type of Text is nvarchar(max) ?
来源:https://stackoverflow.com/questions/2645024/how-to-specify-columntype-in-fluent-nhibernate