Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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:
C#
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:
C#
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:
C#
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:
C#
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
Posted
Updated 23-Jan-15 7:40am
v2
Comments
[no name] 23-Jan-15 13:49pm    
What Problem you have with foreign key, in short please?
Why I ask this: Foreign key (also segmented) has simply match to the referenced primary (it has not be a Primary, but at least a unique key).

Based on the error message you get
Foreign key (FK6482F24702A58C9:Category [fkCategory, pkCategory])) must have same number of columns as the referenced primary key (Skill [pkSkill])

The definition for the foreign key isn't as you described. Instead you have 2 columns in the foreign key definition (fkCategory and pkCategory).

The foreign key creation should look something like
SQL
ALTER TABLE Skill 
ADD FOREIGN KEY (fkCategory)
REFERENCES Category (pkCategory) 
 
Share this answer
 
Comments
agould1 23-Jan-15 13:42pm    
I updated the question with the exact constraints the two tables have. I even dropped the foreign key and readded it using your SQL and still no luck; same exact error
Wendelius 23-Jan-15 13:55pm    
Are you sure that you don't have a foreign key from Category to Skill? The error message implies that the foreign key is referencing to the Skill -table?
agould1 23-Jan-15 14:21pm    
According to sp_help, the only constraint on the Category table is its Primary Key. The table itself is also referenced by the dbo.GroupSkill table, but I haven't mentioned this table since it doesn't seem like it would have any effect on the problem.

Would it help to add the entire table structure?
Wendelius 23-Jan-15 14:30pm    
I believe it could help. Also if you can, check the mappings NHibernate has defined just to make sure that they are ok.
agould1 23-Jan-15 14:46pm    
Found the problem. Turns out there was another attempt at this same problem in the solution but the map was wrong. When I commented out the map it finally worked
Turns out there was another attempt at this same problem in the solution but the map was wrong. Once I commented out this map (as it's essentially a dead map now) everything worked

CSS
using FluentNHibernate.Mapping;
using SkillsMatrix.Domain;

namespace SkillsMatrix.Mapping
{
    public class MatrixSkillMap : ClassMap<matrixskill>
    {
        public MatrixSkillMap()
        {
            Table("Skill");
            Id(x => x.Id, "pkSkill");
            Map(x => x.Name, "Name");
            Map(x => x.CategoryId, "fkCategory");

            Join("Category", c =>
                {
                    c.Fetch.Join();
                    c.KeyColumn("fkCategory", "pkCategory");
                    c.Map(cm => cm.Category, "Name");
                });

            Join("UserSkill", us =>
                {
                    us.Fetch.Join();
                    us.KeyColumn("pkSkill", "fkSkill");
                    us.Map(usm => usm.Rating, "Rating");
                });
        }
    }
}</matrixskill>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900