Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you specifiy the identity column when using the Fluent Nhibernate Table-Per-Subclass strategy?

I am creating a Fluent N hibernate subclass mapping that currently looks something like this:

public class TaskDownloadMap: SubclassMap<TaskDownload>
{
    public TaskDownloadMap()
    {
        Table("TasksDownload");

        Map(x => x.ExtraProperty1, "ExtraProperty1")
            .Nullable();

        Map(x => x.ExtraProperty2, "ExtraProperty2")
            .Nullable();
    }
}

When I try and save one of these entities I get an exception:

Test.TaskRepositoryTest.DeleteTest:
NHibernate.Exceptions.GenericADOException : could not insert: [TaskManager.Entities.TaskDownload#269][SQL: INSERT INTO TasksDownload (ExtraProperty1, ExtraProperty2, Task_id) VALUES (?, ?, ?)]
  ----> System.Data.SqlClient.SqlException : Invalid column name 'Task_id'.

It's because the Id column I have set on my subclass's table is named "TaskId". Is there some to override the default naming scheme that nhibernate is trying to use? I don't seem to have the ability to specify the "Id" column in the subclass and I can't find anyone else even talking about it.

The parent mapping looks like this:

public class TaskMap: ClassMap<Task>
{
    public TaskMap()
    {
        Table("Tasks");

        Id(x => x.Id, "Id")
            .GeneratedBy.Identity();

        .
        .
        .

    }
}

Like I said before I am going with the Table-Per-Subclass strategy so these are 2 different tables. I can change the key on my TasksDownload table to be "Task_id" but I'd rather just be able to specify that it's "TaskId" in my mapping so I can keep to my naming convention.

like image 699
Ben Tidman Avatar asked Feb 02 '12 20:02

Ben Tidman


1 Answers

You cannot configure the Subclass id's with the fluent API because it's handled by the built in mapping convetions. But you can write a custom convention (with some extra acceptance if required).
A sample solution:

public class JoinedSubclassIdConvention : IJoinedSubclassConvention, 
     IJoinedSubclassConventionAcceptance
{
    public void Apply(IJoinedSubclassInstance instance)
    {
         instance.Key.Column(instance.EntityType.BaseType.Name + "Id");
    }

    public void Accept(IAcceptanceCriteria<IJoinedSubclassInspector> criteria)
    {
        criteria.Expect(x => x.EntityType == typeof(TaskDownload));
    }
}

Then you add your convetion to the configuration:

Fluently.Configure()
    //...
    .Mappings(m =>
        {
            m.FluentMappings
            //...
            .Conventions.Add<JoinedSubclassIdConvention>();
        });
like image 139
nemesv Avatar answered Nov 06 '22 00:11

nemesv