So I've looked around quite a bit for assistance to this problem as the error seems somewhat common, but I've exhausted all my options. I've tried several implementations as suggested but I just can't get past it. Here's the exact error:
"Foreign key (FK6482F24702A58C9:Category [fkCategory, pkCategory])) must have same number of columns as the referenced primary key (Skill [pkSkill])"
The idea is that a Category can have many Skills/many Skills can only have one Category
And the code..
Nhibernate Configuration:
public static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005
.ConnectionString(c => c
.FromConnectionStringWithKey("SM_ConnectionString"))
.ShowSql)
.Mappings(m =>
{
m.FluentMappings
.AddFromAssemblyOf<SkillsMatrix>();
m.AutoMappings.Add(
AutoMap.AssemblyOf<SkillsMatrix>(type => type.Namespace.EndsWith("Entities")));
})
.BuildSessionFactory();
}
Category Object:
using System.Collections.Generic;
namespace SkillsMatrix.Entities
{
public class Category
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Skill> Skills { get; set; }
public Category()
{
Skills = new List<Skill>();
}
}
}
Category Map:
using FluentNHibernate.Mapping;
using SkillsMatrix.Entities;
namespace SkillsMatrix.Mapping
{
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Table("Category");
Id(x => x.Id, "pkCategory").GeneratedBy.Identity();
Map(x => x.Name, "Name");
HasMany(x => x.Skills)
.KeyColumn("fkCategory")
.Inverse()
.Cascade.All();
}
}
}
Skill Object:
namespace SkillsMatrix.Entities
{
public class Skill
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual Category Category { get; set; }
}
}
Skill Map:
using FluentNHibernate.Mapping;
using SkillsMatrix.Entities;
namespace SkillsMatrix.Mapping
{
public class SkillMap : ClassMap<Skill>
{
public SkillMap()
{
Table("Skill");
Id(x => x.Id, "pkSkill").GeneratedBy.Identity();
Map(x => x.Name, "Name");
References(x => x.Category)
.Column("pkCategory");
}
}
}
The tables themselves looks like:
Category Table
pkCategory (primary key)
Name
Category Constraints
constraint_type: PRIMARY KEY (clustered)
constraint_keys: pkCategory
Skill Table
pkSkill (primary key)
Name
fkCategory (foreign key to pkCategory)
Skill Constraints
constraint_type: PRIMARY KEY (clustered)
constraint_keys: pkSkill
constraint_type: FOREIGN KEY
constraint_keys: fkCategory REFERENCES dbo.Category (pkCategory)
What am I missing here? It looks like this is a mapping issue, but could it be I have fudged something in creating the sql tables?
Thanks in advance