x.Entries, m => { " /> x.Entries, m => { " /> x.Entries, m => { "/>

NHibernate creates 2 fields in one-to-many relationship

50 Views Asked by At

I have the following entities, mapped by code:

VarRecipeMapping.cs

public VarRecipeMapping()
{
    Table("var_recipe");

    // ...

    Bag(x => x.Entries, m =>
    {
        m.Cascade(Cascade.All | Cascade.DeleteOrphans);
        m.Lazy(CollectionLazy.NoLazy);
    },
    a => a.OneToMany());
}

VarRecipeEntryMapping.cs

public VarRecipeEntryMapping()
{
    Table("var_recipe_entry");

    // ...

    ManyToOne(x => x.Recipe, m =>
    {
        m.Column("var_recipe_id");
        m.NotNullable(true);
    });
}

What annoys me is the resulting database (SQLite): it contains all columns of my entites defined by Column plus an additional, unused Recipe column in the var_recipe_entry table.

Obviously generated by NHibernate due to having a field called Recipe in the model.

What can I do to get rid of this useless field?

1

There are 1 best solutions below

1
kagmole On BEST ANSWER

Found it.

I had to specify the custom name on the "one-to-many" side too.

VarRecipeMapping.cs

public VarRecipeMapping()
{
    Table("var_recipe");

    // ...

    Bag(x => x.Entries, m =>
    {
        // The custom name needs to be specified here too
        m.Key(n => {
            n.Column("var_recipe_id");
        });

        m.Cascade(Cascade.All | Cascade.DeleteOrphans);
        m.Lazy(CollectionLazy.NoLazy);
    },
    a => a.OneToMany());
}

VarRecipeEntryMapping.cs

public VarRecipeEntryMapping()
{
    Table("var_recipe_entry");

    // ...

    ManyToOne(x => x.Recipe, m =>
    {
        m.Column("var_recipe_id");
        m.NotNullable(true);
    });
}