setting type and length on composite-key property in fluent nhibernate
Asked Answered
F

2

6

In hbm mappings I can

<composite-id>
   [..]
   <key-property name="someStringProperty" 
                 column="somefield" 
                 type="AnsiString" 
                 lenght="8"/>
</composite-id>

How do I do that (setting type and length) in Fluent?

Edit:
I posted this on support.fluentnhibernate.org. I included some modifications to support setting the type to e.g. AnsiString there.

Edit 2:
Today Paul Batum has added support for a textual type and a length in his dev-branch. (See github on the changes.)
This makes it possible to write

CompositeId()
  .KeyProperty(
       p => p.SomeProp, 
       k => k.ColumnName("someField").Type("AnsiString").Length(8))
Fuentes answered 12/10, 2010 at 13:41 Comment(3)
I see .ColumnName() and .Type() but not .Length(). Using Fluent NHibernate 1.1.1.694.Dormitory
I guess it hasn't landed in trunk yet.Dormitory
This is beautiful. Thank you. There is no mention of this on the fluent wiki.Southard
B
3

It seems like you can't. You can only go as far as ...

CompositeId()
        .KeyProperty(x => x.Id1, "ID1")
        .KeyProperty(x => x.Id2, "ID2");

There is no option for type or length.

But in version 1.1 there seems to be a possibility

CompositeId() 
            .KeyProperty(x => x.Id1) 
            .KeyProperty(x => x.Id2, kp => kp 
                .ColumnName("ID2") 
                .Type(typeof(string)));
Blackman answered 12/10, 2010 at 13:59 Comment(3)
But the fluent Type(typeof(string)) still results in a nvarchar mapping, whereas the hbm type="AnsiString" results in a varchar mapping!? So there no charge at all?Fuentes
Seems like it. Altough it looks like they could be adding a CustomType to it in the future. Who knows. You might want to start a discussion to add it to the API at support.fluentnhibernate.org.Blackman
Ok. this answer is correct. It is currently impossible to set the length property on composite key parts and the only possible types to set are .net-types. See the edit on the question for a link to more information.Fuentes
P
2

I updated to 1.2 and am able to set the type of a key property to AnsiString

            CompositeId()
            .KeyReference(x => x.ViewDto, "type_id")
            .KeyProperty(x => x.FieldName, p =>
                                               {
                                                   p.ColumnName("field_name");
                                                   p.Type("AnsiString");
                                               });
Phenomenal answered 13/3, 2012 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.