views:

473

answers:

0

Hi everybody,

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!