更新
我发现添加一个单独的键可以使它工作。那么,如果一个属性既不是另一个表的键又不是 ForeignKey 怎么办?
One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'TrafficImageQuestionExtraInfo_TrafficImageQuestion_Source' in relationship 'TrafficImageQuestionExtraInfo_TrafficImageQuestion'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.
public class TrafficImageQuestionExtraInfoMap : EntityTypeConfiguration<TrafficImageQuestionExtraInfo>
{
public TrafficImageQuestionExtraInfoMap()
{
this.HasKey(t => new { t.QuestionId, t.TrafficImageGuid });
this.Property(t => t.TrafficImageGuid).IsRequired();
this.Property(t => t.QuestionId).IsRequired();
this.Property(t => t.Key).IsRequired().HasMaxLength(50);
this.Property(t => t.Value).IsRequired().IsUnicode().HasMaxLength(128);
HasRequired(t => t.TrafficImageQuestion).WithMany(k => k.Properties).HasForeignKey(t => new { t.QuestionId, t.TrafficImageGuid });//.Map(m => m.MapKey("QuestionId", "TrafficImageGuid")).WillCascadeOnDelete();
}
}
和
public class TrafficImageQuestionMap : EntityTypeConfiguration<TrafficImageQuestion>
{
public TrafficImageQuestionMap()
{
// Primary Key
this.HasKey(t => new { t.QuestionId, t.TrafficImageGuid });
// Table & Column Mappings
this.ToTable("TrafficImageQuestions");
this.Property(t=>t.QuestionId).IsRequired();
this.HasRequired(t => t.TrafficImage).
WithMany(t=>t.TrafficImageQuestions).
HasForeignKey(t=>t.TrafficImageGuid).WillCascadeOnDelete();
this.Property(t => t.
Answer).IsRequired();
}
}