Can you specifiy the identity column when using th

2019-06-18 12:24发布

问题:

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.

回答1:

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>();
        });