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!
Best Answer
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.