且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

“外键必须具有与引用的主键相同的列数”。解?

更新时间:2023-02-02 23:25:10

根据您收到的错误消息

外键(FK6482F24702A58C9:类别[fkCategory,pkCategory ]))必须与引用的主键具有相同数量的列(技能[pkSkill])



外键的定义与您所描述的不同。相反,你在外键定义中有2列(fkCategory和pkCategory)。



外键创建应该类似于

  ALTER  技能
ADD FOREIGN KEY (fkCategory)
参考类别(pkCategory)


原来在解决方案中有另一次尝试同样的问题,但地图错了。一旦我评论出这张地图(因为它现在基本上是一张死地图),一切正常工作



 使用  FluentNHibernate  .Mapping; 
使用 SkillsMatrix .Domain;

namespace SkillsMatrix Mapping
{
public class MatrixSkillMap ClassMap< matrixskill>
{
public MatrixSkillMap()
{
Table(Skill);

Id(x => x Id ,pkSkill);
Map(x => x 名称,名称);
Map(x => x CategoryId ,fkCategory);

加入(类别, c =>
{
c 获取 .Join();
c .KeyColumn(fkCategory,pkCategory);
c .Map(cm => cm Category ,Name);
});

加入(UserSkill,我们 =>
{
us Fetch .Join();
us .KeyColumn(pkSkill,fkSkill);
us .Map(usm => usm Rating ,Rating);
});
}
}
} < / matrixskill &gt ; 跨度>


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

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

ALTER TABLE Skill 
ADD FOREIGN KEY (fkCategory)
REFERENCES Category (pkCategory) 


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

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>