“外键必须具有与引用的主键相同的列数”。解? [英] "Foreign Key must have same number of columns as the referenced primary key" solution?

查看:465
本文介绍了“外键必须具有与引用的主键相同的列数”。解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经四处寻找帮助解决这个问题,因为错误看起来有些常见,但我已经筋疲力尽了。我按照建议尝试了几种实现,但我无法超越它。这是确切的错误:



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





想法是一个类别可以有很多技能/许多技能只能有一个类别





和代码..



Nhibernate配置:

  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(< span class =code-string> 实体)));
})
.BuildSessionFactory();
}







类别对象:

 使用 System.Collections.Generic; 

命名空间 SkillsMatrix.Entities
{
public 类别
{
public 虚拟 ID { get ; set ; }
public virtual string 姓名{获取; set ; }
public virtual IList< Skill>技能{获取; set ; }

public 类别()
{
技能= new List< Skill>();
}
}
}





类别地图:

< pre lang =c#> 使用 FluentNHibernate.Mapping;
使用 SkillsMatrix.Entities;

命名空间 SkillsMatrix.Mapping
{
public class CategoryMap:ClassMap< Category>
{
public CategoryMap()
{
Table( 类别);

Id(x = > x.Id, pkCategory)GeneratedBy.Identity();
地图(x = > x.Name, 名称);
HasMany(x = > x.Skills)
.KeyColumn( fkCategory
.Inverse()
.Cascade.All();
}
}
}







技能对象:

 命名空间 SkillsMatrix.Entities 
{
public class 技能
{
public 虚拟 long ID { get ; set ; }
public virtual string 姓名{获取; set ; }
public virtual 类别类别{ get < /跨度>; set ; }
}
}







技能地图:

 使用 FluentNHibernate.Mapping; 
使用 SkillsMatrix.Entities;

命名空间 SkillsMatrix.Mapping
{
public class SkillMap:ClassMap< Skill>
{
public SkillMap()
{
Table( 技能);

Id(x = > x.Id, pkSkill)GeneratedBy.Identity();
地图(x = > x.Name, 名称);

参考(x = > x.Category)
.Column( pkCategory);
}
}
}







桌子本身看起来喜欢:



类别表

pkCategory(主键)

名称< br $> b $ b

类别约束

constraint_type:PRIMARY KEY(聚集)

constraint_keys:pkCategory





技能表

pkSkill(主键)

名称

fkCategory(pkCategory的外键)



技能限制

constraint_type:PRIMARY KEY(群集)

constraint_keys:pkSkill



constraint_type:FOREIGN KEY

constraint_keys :fkCategory REFERENCES dbo.Category(pkCategory)







我在这里缺少什么?看起来这是一个映射问题,但可能是我在创建sql表时捏造了一些东西吗?





提前感谢

解决方案

根据您收到的错误消息

外键(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 > ;


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>


这篇关于“外键必须具有与引用的主键相同的列数”。解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆