-
Notifications
You must be signed in to change notification settings - Fork 65
Description
If a foreign constraint is defined on a table using a unique key constraint instead of a primary key constraint the model generation is not done properly: consider this structure:
-- Referenced Table (Foreign)
CREATE TABLE [dbo].[MasterTable] (
[IdOfRec] INT NOT NULL,
[NaturalKey] NVARCHAR (20) NOT NULL,
PRIMARY KEY CLUSTERED ([IdOfRec] ASC),
CONSTRAINT [IX_UNIQUE] UNIQUE NONCLUSTERED ([NaturalKey] ASC)
);
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_MasterTable]
ON [dbo].[MasterTable]([NaturalKey] ASC);
-- Referencing Table
CREATE TABLE [dbo].[ReferencingTable] (
[Id] INT NOT NULL,
[ReferentialNaturalKey] NVARCHAR (20) NOT NULL,
[Info] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_X] FOREIGN KEY ([ReferentialNaturalKey]) REFERENCES [dbo].[MasterTable] ([NaturalKey])
);
The Classes are:
public class MasterTable {
public virtual int IdOfRec { get; set; }
public virtual string Naturalkey { get; set; }
public virtual ICollection<ReferencingTable> Childs { get; set; }
}
public class ReferencingTable {
public virtual int Id { get; set; }
public virtual string ReferentialNaturalKey { get; set; }
public virtual MasterTable master { get; set; }
public virtual string Info { get; set; }
}
When doing model generation , the collection property Childs should have the property-ref attribute in hbm file. It does not so nhibernate cannot map correctly the relation.