我知道这个问题有一些疑问,但是我找不到正确的答案,所以请让我问这个问题,看看是否有人可以给我正确的答案
我的数据库有以下方案(将表简化为关注该问题)
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
我将那些表编码为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; }
}
以及以下实体的映射
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");
}
}
因此,当我尝试使用以下代码打开会话时,出现错误
"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();
}
那么我应该如何获取映射才能使这项工作呢?
最佳答案
首先,您必须了解该错误,您说:
表分析serialNumber整数PK,FK(引用serialNumber
表项目)
这是错误的,序列号是一个引用serialNumber,idProject和filename的FK,因为这三个一起形成了表格Item PK。这就是为什么错误显示“ ...必须与所引用的主键(Item [fileName,idProject,serialNumber])具有相同的列数”的原因,表Item的主键由三个字段共同组成,而不仅仅是由“ serialNumber如您所建议。
看一下此link,它说明了如何配置复合外键,这是您所需要的。
如果需要任何帮助,请在下面评论。
关于c# - 流利的Nhibernate错误外键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49735958/