简体   繁体   English

流利的Nhibernate错误外键

[英]Fluent Nhibernate error Foreign Key

I know that this issue has some questions about it, but I can't find the right answer, so please let me ask this question to see if someone could give me the right answer 我知道这个问题有一些问题,但我找不到合适的答案,所以请让我问这个问题,看看是否有人能给我正确的答案

I have the following scheme for my DB (tables an dsimplified to focus on the problem) 我的数据库有以下方案(表格是dsimplified,专注于问题)

Table Project
idProject INT PK
projectName string UNIQUE
numOfItems INT 

Table Item
serialNumber integer PK
idProject integer PK, FK (references idProject table Project)
fileName string PK

Table Analysis
serialNumber integer PK, FK (references serialNumber table Item)
dateMeasure Date PK
fileName string PK

I have those tables coded in C# as follows 我有这些用C#编码的表格如下

class Analysis{
        public virtual Item serialNum{ get; set; }
        public virtual DateTime dateMeasure { get; set; }
        public virtual string fileName { get; set; }

        public override int GetHashCode(){
             return (fileName.GetHashCode() * serialNum.GetHashCode() * dateMeasure.GetHashCode());
        }

        public override bool Equals(object obj){
            if (obj == null || obj.GetType() != GetType()) return false;

            Analysis a = (Analysis)obj;

            return (a.serialNum == serialNum && a.fileName == fileName && a.dateMeasure == dateMeasure);
        }
}

class Item{
        public virtual int serialNumber { get; set; }
        public virtual Proyecto idProject { get; set; }
        public virtual string fileName { get; set; }
        public virtual DateTime measureDate { get; set; }

        public override int GetHashCode(){
            return (fileName.GetHashCode() * serialNumber.GetHashCode() * idProject.GetHashCode());
        }

        public override bool Equals(object obj)
        {
            if (obj == null || obj.GetType() != GetType()) return false;

            Item i = (Item)obj;

            return (i.serialNumber == serialNumber&& i.fileName== fileName&& d.idProject == idProject);
        }
}

class Project
    {
        public virtual int idProject { get; set; }
        public virtual string projectName{ get; set; }
        public virtual int numItems { get; set; }
    }

And the following mappings of the entites 和以下的entites映射

class AnalysisMap: ClassMap<Analysis>
    {

        public AnalysisMap()
        {
            CompositeId()
                .KeyReference(x=> x.serialNumber)
                .KeyProperty(x => x.dateMeasure)
                .KeyProperty(x => x.fileName);
        }
}

class ItemMap : ClassMap<Item>
    {
        public ItemMap()
        {
            CompositeId()
                .KeyProperty(x => x.fileName)
                .KeyReference(x => x.idProject)
                .KeyProperty(x => x.serialNumber);
            Map (x=>x.measureDate).Column("dateMeasure").Not.Nullable();
            References(x => x.idProject).Column("idProject");
        }
    }

class ProjectMap : ClassMap<Project>
    {
        public ProjectMap()
        {
            Id(x => x.idProject).GeneratedBy.Identity().Column("idProject");
            Map(x => x.projectName).Column("projectName").Unique();
            Map(x => x.NumItems).Column("numOfItems").Not.Nullable().Default("0");
        }
    }

So when I tried to open the session with the following code I get the error "Foreign key (FK9CF1483E7BAABE07:Analysis [serialNum])) must have same number of columns as the referenced primary key (Item [fileName, idProject, serialNumber])" 因此,当我尝试使用以下代码打开会话时,我收到错误"Foreign key (FK9CF1483E7BAABE07:Analysis [serialNum])) must have same number of columns as the referenced primary key (Item [fileName, idProject, serialNumber])"

try{
   ISessionFactory sf = Fluently.Configure()
   .Database(SQLiteConfiguration.Standard.InMemory().ShowSql()
   .ConnectionString("server=local;Data Source= data_source;Integrated Security=SSPI;"))
   .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Project>()
   .AddFromAssemblyOf<Item>()
   .AddFromAssemblyOf<Analysis>()).BuildSessionFactory();

   ISession session = sf.OpenSession();
   lblStatus.Text = "OK";
}
catch (Exception ex){
   lblStatus.Text = ex.Message.ToString();
}

So how should I get the mapping in order to make this work?? 那么我该如何获得映射才能使这个工作?

First you got to understand the error, you said: 首先,你必须了解错误,你说:

Table Analysis serialNumber integer PK, FK (references serialNumber table Item) 表分析serialNumber整数PK,FK(引用serialNumber表项)

This is wrong, serial number is a FK that references serialNumber, idProject and filename, because the three together forms table Item PK. 这是错误的,序列号是引用serialNumber,idProject和filename的FK,因为三者一起形成表项PK。 That's why the error says "...must have same number of columns as the referenced primary key (Item [fileName, idProject, serialNumber])", the primary key of table Item is composed by three fields together and not only by "serialNumber" as you have suggested. 这就是错误说“...必须与引用的主键具有相同数量的列(Item [fileName,idProject,serialNumber])”的原因,表Item的主键由三个字段组成,而不仅仅是“serialNumber” “正如你所建议的那样。

Take a look at this link , it explains how to configure a composite foreign key, this is what you need. 看一下这个链接 ,它解释了如何配置复合外键,这就是你需要的。

Comment bellow if you need any help. 如果您需要任何帮助,请留言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM