使用 Fluent-nhibernate 自动映射在外键上添加多列唯一约束
我是一个 NHibernate 和流畅的 NHibernate 新手。我在唯一约束和 nhibernate 映射方面遇到了一些问题。
我有域模型的以下部分。
public class Batch
{
public virtual int Id {get; set;}
public virtual string Name {get; set;}
public virtual IList<BatchParameter> BatchParameters {get; set;}
}
public class BatchParameter
{
public virtual int Id {get; set;}
public virtual string Name {get; set;}
public virtual Batch Batch {get; set;}
}
我正在尝试使用 Fluent-nhibernate 使用自动映射将其映射到数据库(SQLServer)上。 我想设置我的数据库,以便具有:
“Id”属性上的主键
BatchParamets 表上的外键
p>Batch 表中 Name 列的唯一约束
BatchParameters 表中 Name 和列的唯一约束Batch_Id
所以我写下了以下代码:
public class BatchMapping : IAutoMappingOverride<Batch>
{
public void Override(FluentNHibernate.Automapping.AutoMapping<Batch> mapping)
{
mapping.Id( b => b.Id);
mapping.HasMany<BatchParameter>(p => p.BatchParameters).Cascade.All().Inverse();
}
}
public class BatchParameterMapping : IAutoMappingOverride<BatchParameter>
{
public void Override(FluentNHibernate.Automapping.AutoMapping<BatchParameter> mapping)
{
mapping.Id( b => b.Id);
mapping.Map(b => b.Name).Unique();
//mapping.Map(p => p.Name).UniqueKey("Batch_Parameter");
//mapping.Map(p => p.Batch.Id).UniqueKey("Batch_Parameter");
}
}
主键、外键和第一个唯一约束没有问题。对于唯一约束有点头疼。
有人可以告诉我直接的方法吗???
谢谢!
I'm an NHibernate and fluent-nhibernate newbie. And I've got some problem with unique constraint and nhibernate mapping.
I've got the following part of domain model.
public class Batch
{
public virtual int Id {get; set;}
public virtual string Name {get; set;}
public virtual IList<BatchParameter> BatchParameters {get; set;}
}
public class BatchParameter
{
public virtual int Id {get; set;}
public virtual string Name {get; set;}
public virtual Batch Batch {get; set;}
}
I'm trying to use fluent-nhibernate to map it on the db (SQLServer) using automapping.
I want to be set up my db in order to have :
Primary Keys on the "Id"s properties
a Foreign Key on the BatchParamets table
a Unique Constraint on the Batch table on column Name
a Unique Constraint on the BatchParameters table on columns Name and Batch_Id
So I've written down this code:
public class BatchMapping : IAutoMappingOverride<Batch>
{
public void Override(FluentNHibernate.Automapping.AutoMapping<Batch> mapping)
{
mapping.Id( b => b.Id);
mapping.HasMany<BatchParameter>(p => p.BatchParameters).Cascade.All().Inverse();
}
}
public class BatchParameterMapping : IAutoMappingOverride<BatchParameter>
{
public void Override(FluentNHibernate.Automapping.AutoMapping<BatchParameter> mapping)
{
mapping.Id( b => b.Id);
mapping.Map(b => b.Name).Unique();
//mapping.Map(p => p.Name).UniqueKey("Batch_Parameter");
//mapping.Map(p => p.Batch.Id).UniqueKey("Batch_Parameter");
}
}
No problems for the primary keys, the foreign key and the first Unique Constraint. A little bit of headache for the Unique Constraint.
Can someone show me the straight way???
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您似乎遇到了复制粘贴错误:
...Map(b => b.Name)...
应该放入BatchMapping
,不是BatchParameterMapping。接下来,
BatchParameter.Batch
是从BatchParameter
到Batch
的多对一关系,因此应该使用References( ...)
而不是Map(...)
。您可以使用References
作为另一个实体的外键,并使用Map
作为简单属性。最后,您应该删除
Id
属性和Batch.BatchParameters
的不必要映射。默认情况下,Fluent NHibernate 的自动映射将根据需要映射它们。在Override
方法中,您只需指定要执行与自动映射默认值不同的操作的属性,例如指定唯一键。First, it looks like you have a copy-and-paste error:
...Map(b => b.Name)...
should go inBatchMapping
, notBatchParameterMapping
.Next,
BatchParameter.Batch
is a many-to-one relationship fromBatchParameter
toBatch
, so it should be mapped withReferences(...)
instead ofMap(...)
. You useReferences
for foreign keys to another entity and useMap
for simple properties.Finally, you should remove the unnecessary mappings for the
Id
properties andBatch.BatchParameters
. Fluent NHibernate's auto-mapping will map them as desired by default. In yourOverride
methods you only need to specify the properties where you want to do something differently than the auto-mapping default, such as specifying unique keys.如果 Id 和 Name 是 BatchParameter 表中的主键,则您将需要一个复合 id。另外,如果您想从 BatchParameter 类返回 Batch 的引用,则需要使用 Reference。以下内容应该接近您的需要:
If Id and Name are primary keys in your BatchParameter table you would need a composite id. Also if you want to have a reference back to Batch from your BatchParameter class you will need to use Reference. The following should be close to what you need: