I have an pretty big database and I have to map two entities using NHibernate 3.3.1 byCode mapping (please take in account that I'm not using FluentNHibernate, but built-in mappings)
Entities are in a many to many relationship through and unique key, not primary key.
Here are my examples
public class Achievable : Entity
{
public Achievable()
{
Uniqueidentifier = Guid.NewGuid();
}
public virtual string Name { get; set; }
public virtual Guid Uniqueidentifier { get; set; }
public virtual IList<Kitting> Kittings { get; set; }
}
public class Kitting : Entity
{
public virtual string Name { get; set; }
public virtual IList<Achievable> Achievables { get; set; }
}
So I have to link Achievable table to Kitting using third table AchievableKittings through Uniqueidentifier property
I've written the following mapping classes :
public class AchievableMap : ClassMapping<Achievable>
{
public AchievableMap()
{
Property(x => x.Name);
Property(x => x.Uniqueidentifier, m =>
{
m.Unique(true);
m.NotNullable(true);
});
Bag(x => x.Kittings, m =>
{
m.Table("AchievableKittings");
m.Cascade(Cascade.Persist);
m.Inverse(false);
m.Key(k =>
{
k.PropertyRef(x => x.Uniqueidentifier);
k.Column("aUniqueindentifier");
k.ForeignKey("FK_Achievable_uniqueidentifier");
});
}, r => r.ManyToMany(m =>
{
m.Column("kittingId");
m.ForeignKey("FK_Kitting_kittingId");
}));
}
}
public class KittingMap : ClassMapping<Kitting>
{
public KittingMap()
{
Property(x => x.Name);
Bag(x => x.Achievables, m =>
{
m.Table("AchievableKittings");
m.Inverse(true);
m.Key(k =>
{
k.Column("kittingId");
k.ForeignKey("FK_Kitting_kittingId");
});
}, r => r.ManyToMany(m =>
{
m.Column("aUniqueindentifier");
m.ForeignKey("FK_Achievable_uniqueidentifier");
}));
}
}
It seams that NHibernate ignore k.PropertyRef(x => x.Uniqueidentifier) statement. My Primary keys are bigint generated by identity. Now when Nhiebrnate tries to create the db its create the FK key for AchievableKittings to primaryKey of achievable, but not property-ref Uniqueidentifier
alter table AchievableKittings
add constraint FK_Achievable_uniqueidentifier
foreign key (aUniqueindentifier)
references Achievable
and of course its throws an exception:
Column 'Achievable.AchievableId' is not the same data type as referencing column 'AchievableKittings.aUniqueindentifier' in foreign key 'FK_Achievable_uniqueidentifier'.
Using FluentNhibernate is was able to map this in a right way. Unfortunately I can not use FluentNhibernate in current project.
So could some one help me with this issue.