Fluent NHibernate — specify table/column names when using HiLo generator - fluent-nhibernate

Yes, nit-picky is a good way to describe this... ;)
Does anyone know if you can specify the table and/or column names to use HiLo using Fluent NHibernate? Currently the schema export creates this...
create table hibernate_unique_key (
next_hi INTEGER
)

Figured it out...
public class IdGenerationConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.GeneratedBy.HiLo("MyTableName", "NextHighValue", "1000");
}
}

Related

Override Id column's auto-increment initial value with Fluent nHibernate AutoMapping

I'm using Fluent nHibernate AutoMapping for a project and would like to change the initial Id value for one table in my schema from 1 to say 1000 (all other tables I would like to remain with the default behaviour of starting the Id at 1).
I'm guessing that I need to specify this in my IIDConvetion, but am not sure how?
public class PrimaryKeyConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.Column(instance.EntityType.Name + "Id");
instance.GeneratedBy.Native();
}
}
Would really appreciate being pointed in the right direction.
AFAIK this is not supported yet. Your best bet is to implement IAuxiliaryDatabaseObject and set the starting value of the identity to 1000 with sql then add it as last element to the configuration object.

Fluent NHibernate: How do you change the underlying sql type for an automapped collection of strings?

I have a class:
public class ParentClass
{
//other stuff
IList<string> ChildPages
}
When 'IList<string> ChildPages' gets automapped by Fluent NHibernate a 'ChildPages' join table is created with a nvarchar(255) backing field for each string in the collection.
But the problem is that I want the sql backing field to be 'text', so that I can have lengthy entries for this entity.
What's the easiest way to make this change?
How do you change the underlying type of automapped primitive collections?
Also, for extra points, how would you do this with a convention or mapping overrides?
Thx!
You can setup an override convention so that strings use text instead of nvarchar2 (255), as shown in the solution on this other thread. If you only want such behavior for a specific property, something like this in your fluent configuration would work:
AutoMap
.AssemblyOf<ParentClass>()
.Override<ChildPage>(map =>
{
mapping.Map(x => x.Page).CustomType("StringClob").CustomSqlType("text");
});

Fluent NHibernate different Conventions for different base types

At this moment we are keeping all Entities and Mappings into same assembly. Our entities derived from a basic class Entity which is an EntityWithTypedId
Also we are having a table name Convention telling to pluralize the table names.
Now I want to create other two base types e.q. AggregateRootEntity and AggregateEntity, both derive from Entity.
And I would like to create two set of conventions for both base entities:
Let's say:
For for all entities derived from AggregateRootEntity tables should be prefixed with "ag_" and Id is incremental generated, but for all entities derived from AggregateEntity tables should be prefixed with "a_" and Ids should be assigned.
Is it possible to Set Conventions based on some conditions?
You can do it with multiple conventions, each checking for a specific type in their Accept methods
something like:
public class LegacyEntityTableConvention : IClassConvention, IClassConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
{
criteria.Expect(x => x.EntityType.IsAny(typeof(OldClass), typeof(AnotherOldClass)));
}
public void Apply(IClassInstance instance)
{
instance.Table("tbl_" + instance.EntityType.Name);
}
}
Just a block of code out of the FNH Wiki
http://wiki.fluentnhibernate.org/Acceptance_criteria

Is it possible to have a OneToOne relation to a class hierarchy persisted using JoinedBase?

I'd like one of my entities to have a one-to-one relationship with a class hierarchy. Think of it like a Strategy pattern, where each strategy needs different parameters to be persisted. I tried using a combination of OneToOne and JoinedBase/JoinedKey, but I've come across a problem.
With this combination, the primary key of the main entity also appears as the primary key of the table representing the root class in the hierarchy, and as the primary key of the subclass:
Order --------------- TaxCalculator
([PrimaryKey]Id = 1234) ([PrimaryKey(PrimaryKeyType.Foreign)]OrderId = 1234)
^
|
|
UkTaxCalculator
([JoinedKey]UkTaxCalculatorId = 1234)
I can persist this fine, but then I can't change which subclass of TaxCalculator I have. When I do something like:
order.TaxCalculator = new OverseasTaxCalculator(order);
then try to flush, then ActiveRecord/NHibernate (understandably) gets unhappy that there are now two TaxCalculators with Id = 1234.
I can get around this by replacing the OneToOne with a HasMany/BelongsTo, and hiding the multiplicity from users of the Order object, but I'm interested to know if it's possible to do this with OneToOne.
There's a full code example on github. This code throws an exception when the second SessionScope is disposed. If you clone the project, it should run out-of-the-box.
first of all i am sorry, but i did not tried my solution. It is to late and i really need my sleep ;-). I think the only way the one-to-one could work would be a 'table-per-hierarchy'-approach using a discriminator column instead of table-per-subclass. Maybe this will enable you to morph the existing object to another subclass. An other way, something like a polymorphic delete-orphan unfortunately is not supported as you stated. So i'll guess this would be your (very) last option.
But if this fails why don't you map it as a one-to-many instead of many-to-one with a foreign key in the order table, reusing the TaxCalculators? I would imagine them as quite static.
Interesting idea though: polymorphic delete-orphan.
We do something very similar to what you are trying to do. I think it's your combination of one-to-one and the joined key that's causing the problem. Try this:
[ActiveRecord, JoinedBase]
public class TaxCalculator
{
protected int TaxCalculatorId;
[PrimaryKey]
public virtual int Id
{
get { return TaxCalculatorId; }
set { TaxCalculatorId = value; }
}
// common tax calculation fields, methods etc...
}
[ActiveRecord]
public class OverseasTaxCalculator : TaxCalculator
{
[JoinedKey]
public override int Id
{
get { return TaxCalculatorId; }
set { TaxCalculatorId = value; }
}
// overseas tax calculation specific methods, properties etc...
}

fluent nhibernate automap version column

Current code in my convention:
public void Apply(FluentNHibernate.Conventions.Instances.IVersionInstance instance)
{
instance.Column("RowVersion");
instance.Not.Nullable();
instance.UnsavedValue("0");
instance.Default(1);
}
This doesn't render the RowVersion as a version column. It treats RowVersion as just another column.
So my question is how do you add Row Version Concurrency using Fluent Nhibernate?
The VersionStep class only looks for properties that match the names of Version or Timestamp. Your property will have to match that name. Replacing VersionStep to change the naming rules is another option.