在使用Fluent Nhibernate Table-Per-Subclass策略时,您可以指定标识列吗? [英] Can you specifiy the identity column when using the Fluent Nhibernate Table-Per-Subclass strategy?
问题描述
public class TaskDownloadMap:SubclassMap< TaskDownload> ;
{
public TaskDownloadMap()
{
表(TasksDownload);
Map(x => x.ExtraProperty1,ExtraProperty1)
.Nullable();
Map(x => x.ExtraProperty2,ExtraProperty2)
.Nullable();
$ b当我尝试保存这些实体时,一个例外:
Test.TaskRepositoryTest.DeleteTest:
NHibernate.Exceptions.GenericADOException:无法插入:[TaskManager。 (?,?,?)]
----> System.Data.SqlClient.SqlException:无效的列名称Task_id。
这是因为我在子类表上设置的Id列名为TaskId。有一些重写nhibernate试图使用的默认命名方案吗?我似乎没有能力在子类中指定ID列,我甚至找不到任何人谈论它。
父映射看起来像这样:
public class TaskMap:ClassMap< Task>
{
public TaskMap()
{
表(任务);
Id(x => x.Id,Id)
.GeneratedBy.Identity();
。
。
。
就像我之前说过的,使用Table-Per-Subclass策略,所以这些是2个不同的表。我可以将我的TasksDownload表上的键改为Task_id,但我宁愿只是在映射中指定它为TaskId,这样我就可以遵守命名约定。
解决方案你不能用流利的API配置Subclass id,因为它是由内置的映射处理来处理的。但是你可以写一个自定义大会(带有一些额外的 acceptance )。
样本解决方案:
public class JoinedSubclassIdConvention:IJoinedSubclassConvention,
IJoinedSubclassConventionAcceptance
{
public void Apply(IJoinedSubclassInstance instance)
{
instance.Key.Column(instance.EntityType .BaseType.Name +Id);
$ b public void Accept(IAcceptanceCriteria< IJoinedSubclassInspector> criteria)
{
criteria.Expect(x => x.EntityType == typeof(TaskDownload)) ;
$ b然后你把你的传递添加到配置中: p>
流利.Configure()
// ...
.Mappings(m =>
{
m.FluentMappings
// ...
.Conventions.Add< JoinedSubclassIdConvention>();
});
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.
解决方案 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>();
});
这篇关于在使用Fluent Nhibernate Table-Per-Subclass策略时,您可以指定标识列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!