Find base column size using NHibernate metadata

Is there a way to use, SessionFactory.GetClassMetadata()or any other method that you know about, to dynamically get the maximum size of the column varcharthat underlies the row property of the NHibernate class?

To clarify, I do not want to read the length attribute specified in the NHibernate mapping file. I want to infer the actual length of the database column.

+5
source share
2 answers

See the code below for two different ways to get the column size for a row from NHib metadata.

Cheers,
Berryl

    [Test]
    public void StringLength_DefaultIs_50_v1()
    {
        _metadata = _SessionFactory.GetClassMetadata(typeof(User));
        var propertyType = _metadata.GetPropertyType("Email") as StringType;
        Assert.That(propertyType.SqlType.Length, Is.EqualTo(50));
    }

    [Test]
    public void StringLength_DefaultIs_50_v2()
    {
        var mapping = _Cfg.GetClassMapping(typeof(User));
        var col = mapping.Table.GetColumn(new Column("Email"));
        Assert.That(col.Length, Is.EqualTo(50));
    }
+6
source

factory , NH ( ), . "" , , , (.. * sys.columns..... sql-), .

, NH (, , (n) varchar)

+1

All Articles